1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Policies; |
13
|
|
|
|
14
|
|
|
use Tinyissue\Contracts\Model\UserInterface; |
15
|
|
|
use Tinyissue\Extensions\Policies\AdminAccess; |
16
|
|
|
use Tinyissue\Extensions\Policies\ProjectAccess; |
17
|
|
|
use Tinyissue\Model\Project; |
18
|
|
|
use Tinyissue\Model\User; |
19
|
|
|
use Tinyissue\Model\Project\Note; |
20
|
|
|
use Illuminate\Auth\Access\HandlesAuthorization; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class NotePolicy. |
24
|
|
|
* View: member of the project and manager role. |
25
|
|
|
* Create: admin role. |
26
|
|
|
* Update: admin role. |
27
|
|
|
* Delete: admin role. |
28
|
|
|
* |
29
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class NotePolicy |
32
|
|
|
{ |
33
|
|
|
use HandlesAuthorization, ProjectAccess; |
34
|
|
|
|
35
|
|
View Code Duplication |
public function before(UserInterface $user) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$this->dd(__METHOD__); |
|
|
|
|
38
|
|
|
if ($user instanceof UserInterface && ($user->isAdmin() || $user->isManager())) { |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Determine whether the user can view the note. |
45
|
|
|
* |
46
|
|
|
* @param User $user |
47
|
|
|
* @param Note $note |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
// public function view(User $user, Note $note, Project $project) |
51
|
|
|
// { |
52
|
|
|
// if ($this->isPublicProject($project) || $project->isMember($user->id)) { |
53
|
|
|
// return true; |
54
|
|
|
// } |
55
|
|
|
// |
56
|
|
|
// return false; |
57
|
|
|
// } |
58
|
|
View Code Duplication |
public function view(User $user, Project $project) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->dd(__METHOD__); |
|
|
|
|
61
|
|
|
if ($this->isPublicProject($project) || $project->isMember($user->id)) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Determine whether the user can create notes. |
70
|
|
|
* |
71
|
|
|
* @param User $user |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function create(User $user) |
75
|
|
|
{ |
76
|
|
|
$this->dd(__METHOD__); |
|
|
|
|
77
|
|
|
return $user->isManager(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Determine whether the user can update the note. |
82
|
|
|
* |
83
|
|
|
* @param User $user |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function update(User $user) |
87
|
|
|
{ |
88
|
|
|
$this->dd(__METHOD__); |
|
|
|
|
89
|
|
|
return $this->create($user); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Determine whether the user can delete the note. |
94
|
|
|
* |
95
|
|
|
* @param User $user |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function delete(User $user) |
99
|
|
|
{ |
100
|
|
|
$this->dd(__METHOD__); |
|
|
|
|
101
|
|
|
return $this->create($user); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
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.