diff --git a/app/Repositories/CacheApiScopeRepository.php b/app/Repositories/CacheApiScopeRepository.php index d1e60473..2fc6e4d9 100644 --- a/app/Repositories/CacheApiScopeRepository.php +++ b/app/Repositories/CacheApiScopeRepository.php @@ -70,4 +70,15 @@ final class CacheApiScopeRepository extends BaseCacheRepository implements IApiS return $this->repository->getAssignableByGroups(); }); } + + /** + * @param string $scope_name + * @return IApiScope + */ + public function getFirstByName($scope_name) + { + return Cache::remember($this->cache_base_key.'_'.$scope_name, $this->cache_minutes_lifetime, function() use($scope_name) { + return $this->repository->getFirstByName($scope_name); + }); + } } \ No newline at end of file diff --git a/app/Repositories/EloquentApiScopeRepository.php b/app/Repositories/EloquentApiScopeRepository.php index 01cc87fd..350fc894 100644 --- a/app/Repositories/EloquentApiScopeRepository.php +++ b/app/Repositories/EloquentApiScopeRepository.php @@ -69,4 +69,13 @@ final class EloquentApiScopeRepository extends AbstractEloquentEntityRepository ->where('assigned_by_groups', '=', true) ->orderBy('api_id')->get(); } + + /** + * @param string $scope_name + * @return IApiScope + */ + public function getFirstByName($scope_name) + { + return $this->entity->where('active', '=', true)->where('name', $scope_name)->first(); + } } \ No newline at end of file diff --git a/app/Services/OAuth2/ApiScopeService.php b/app/Services/OAuth2/ApiScopeService.php index 01818e52..d6704fee 100644 --- a/app/Services/OAuth2/ApiScopeService.php +++ b/app/Services/OAuth2/ApiScopeService.php @@ -160,7 +160,7 @@ final class ApiScopeService implements IApiScopeService if ($param == 'name') { //check if we have a former scope with selected name - $former_scope = $this->repository->getByName($params[$param]); + $former_scope = $this->repository->getFirstByName($params[$param]); if (!is_null($former_scope) && $former_scope->id != $id) { throw new InvalidApiScope(sprintf('scope name %s already exists!', $params[$param])); diff --git a/app/libs/OAuth2/Repositories/IApiScopeRepository.php b/app/libs/OAuth2/Repositories/IApiScopeRepository.php index 4602e1a8..7f8b7e48 100644 --- a/app/libs/OAuth2/Repositories/IApiScopeRepository.php +++ b/app/libs/OAuth2/Repositories/IApiScopeRepository.php @@ -25,6 +25,12 @@ interface IApiScopeRepository extends IBaseRepository */ public function getByName(array $scopes_names); + /** + * @param string $scope_name + * @return IApiScope + */ + public function getFirstByName($scope_name); + /** * @return IApiScope[] */