Completed
Push — master ( 3a6bc2...bdf328 )
by Conrad
01:56
created

UserEntity::hasScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace AdvancedLearning\Oauth2Server\Entities;
4
5
use AdvancedLearning\Oauth2Server\Extensions\GroupExtension;
6
use League\OAuth2\Server\Entities\Traits\EntityTrait;
7
use League\OAuth2\Server\Entities\UserEntityInterface;
8
use SilverStripe\Security\Group;
9
use SilverStripe\Security\Member;
10
11
class UserEntity implements UserEntityInterface
12
{
13
    use EntityTrait;
14
15
    protected $member;
16
17
    public function __construct(Member $member)
18
    {
19
        $this->member = $member;
20
        $this->setIdentifier($member->Email);
21
    }
22
23
    /**
24
     * Get the Member associated with this ClientEntity.
25
     *
26
     * @return Member
27
     */
28
    public function getMember()
29
    {
30
        return $this->member;
31
    }
32
33
    /**
34
     * Checks whether the member has a scope. Only works if the GroupExtension has been configured.
35
     *
36
     * @param string $scope
37
     * @return bool
38
     */
39
    public function hasScope(string $scope): bool
40
    {
41
        // always return true if extensions not configured
42
        return !Group::create()->hasExtension(GroupExtension::class) || $this->getMember()->Groups()->filter([
43
            'Scopes.Name' => $scope
44
        ])->count() > 0;
45
    }
46
}
47