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()
62
62
$ this ->scopes = new \Doctrine \Common \Collections \ArrayCollection ();
63
63
}
64
64
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
+
65
80
/**
66
81
* Set name.
67
82
*
Original file line number Diff line number Diff line change 3
3
namespace ApiSkeletonsTest \Laravel \Doctrine \ApiKey \Feature \Entity ;
4
4
5
5
use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \ApiKey ;
6
+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \Scope ;
6
7
use ApiSkeletons \Laravel \Doctrine \ApiKey \Http \Middleware \AuthorizeApiKey ;
7
8
use ApiSkeletons \Laravel \Doctrine \ApiKey \Service \ApiKeyService ;
8
9
use ApiSkeletonsTest \Laravel \Doctrine \ApiKey \TestCase ;
@@ -60,4 +61,21 @@ public function testAccessEvent(): void
60
61
61
62
$ this ->assertEquals (0 , count ($ apiKey ->getAccessEvents ()));
62
63
}
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
+ }
63
81
}
You can’t perform that action at this time.
0 commit comments