File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,21 @@ public function __construct()
6262 $ this ->scopes = new \Doctrine \Common \Collections \ArrayCollection ();
6363 }
6464
65+ /**
66+ * A convenience function to check if a scope is assigned to the apikey
67+ * by name
68+ */
69+ public function hasScope (string $ scopeName ): bool
70+ {
71+ foreach ($ this ->getScopes () as $ scope ) {
72+ if ($ scope ->getName () === $ scopeName ) {
73+ return true ;
74+ }
75+ }
76+
77+ return false ;
78+ }
79+
6580 /**
6681 * Set name.
6782 *
Original file line number Diff line number Diff line change 33namespace ApiSkeletonsTest \Laravel \Doctrine \ApiKey \Feature \Entity ;
44
55use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \ApiKey ;
6+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \Scope ;
67use ApiSkeletons \Laravel \Doctrine \ApiKey \Http \Middleware \AuthorizeApiKey ;
78use ApiSkeletons \Laravel \Doctrine \ApiKey \Service \ApiKeyService ;
89use ApiSkeletonsTest \Laravel \Doctrine \ApiKey \TestCase ;
@@ -60,4 +61,21 @@ public function testAccessEvent(): void
6061
6162 $ this ->assertEquals (0 , count ($ apiKey ->getAccessEvents ()));
6263 }
64+
65+ public function testHasScope (): void
66+ {
67+ $ entityManager = $ this ->createDatabase (app ('em ' ));
68+ $ apiKeyRepository = $ entityManager ->getRepository (ApiKey::class);
69+ $ scopeRepository = $ entityManager ->getRepository (Scope::class);
70+
71+ $ apiKey = $ apiKeyRepository ->generate ('testing ' );
72+ $ scope = $ scopeRepository ->generate ('scopetest ' );
73+ $ entityManager ->flush ();
74+
75+ $ apiKeyRepository ->addScope ($ apiKey , $ scope );
76+ $ entityManager ->flush ();
77+
78+ $ this ->assertTrue ($ apiKey ->hasScope ('scopetest ' ));
79+ $ this ->assertFalse ($ apiKey ->hasScope ('fail ' ));
80+ }
6381}
You can’t perform that action at this time.
0 commit comments