Completed
Pull Request — master (#6)
by Christopher
15:01 queued 06:11
created

UserAuthoriser::createableBy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace TechWilk\Rota\Authoriser;
4
5
use TechWilk\Rota\AuthoriserInterface;
6
use TechWilk\Rota\GroupQuery;
7
use TechWilk\Rota\User;
8
9
class UserAuthoriser implements AuthoriserInterface
10
{
11
    private $user;
12
13
    public function __construct(User $user)
14
    {
15
        $this->user = $user;
16
    }
17
18
    public function readableBy(User $user)
19
    {
20
        // it's me!
21
        if ($this->user === $user) {
22
            return true;
23
        }
24
        // we're a superadmin
25
        if ($user->isAdmin()) {
26
            return true;
27
        }
28
        // we're in the same group
29
        $myGroups = GroupQuery::create()
30
            ->useRoleQuery()
31
                ->useUserRoleQuery()
32
                    ->filterByUser($user)
33
                ->endUse()
34
            ->endUse()
35
            ->find();
36
        $sameGroups = GroupQuery::create()
37
            ->useRoleQuery()
38
                ->useUserRoleQuery()
39
                    ->filterByUser($this->user)
40
                ->endUse()
41
                ->filterByGroup($myGroups)
42
            ->endUse()
43
            ->count();
44
        if ($sameGroups > 0) {
45
            return true;
46
        }
47
48
        return false;
49
    }
50
51
    public static function createableBy(User $user)
52
    {
53
        // we're a superadmin
54
        if ($user->isAdmin()) {
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61 View Code Duplication
    public function updatableBy(User $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        // it's me!
64
        if ($this->user === $user) {
65
            return true;
66
        }
67
        // we're a superadmin
68
        if ($user->isAdmin()) {
69
            return true;
70
        }
71
72
        return false;
73
    }
74
75 View Code Duplication
    public function deteteableBy(User $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        // it's me!
78
        if ($this->user === $user) {
79
            return true;
80
        }
81
        // we're a superadmin
82
        if ($user->isAdmin()) {
83
            return true;
84
        }
85
86
        return false;
87
    }
88
}
89