|
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\ProjectAccess; |
|
16
|
|
|
use Tinyissue\Model\Project; |
|
17
|
|
|
use Tinyissue\Model\User; |
|
18
|
|
|
use Tinyissue\Model\Project\Issue\Comment; |
|
19
|
|
|
use Illuminate\Auth\Access\HandlesAuthorization; |
|
20
|
|
|
use Illuminate\Support\Facades\Gate; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class CommentPolicy. |
|
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 CommentPolicy |
|
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 comment. |
|
45
|
|
|
* |
|
46
|
|
|
* @param User $user |
|
47
|
|
|
* @param Comment $comment |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
public function view(User $user, Comment $comment) |
|
52
|
|
|
{ |
|
53
|
|
|
return Gate::forUser($user)->allows('view', [$comment->issue, $comment->issue->project]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Determine whether the user can create comments. |
|
58
|
|
|
* |
|
59
|
|
|
* @param User $user |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
public function create(User $user, $comment, Project $project) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
return $project->isMember($user->id); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Determine whether the user can update the comment. |
|
70
|
|
|
* |
|
71
|
|
|
* @param User $user |
|
72
|
|
|
* @param Comment $comment |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function update(User $user, Comment $comment) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->dd(__METHOD__); |
|
|
|
|
|
|
79
|
|
|
return $user->id === $comment->created_by || ($this->view($user, $comment) && $user->isManagerOrMore()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Determine whether the user can delete the comment. |
|
84
|
|
|
* |
|
85
|
|
|
* @param User $user |
|
86
|
|
|
* @param Comment $comment |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
|
|
public function delete(User $user, Comment $comment) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->dd(__METHOD__); |
|
|
|
|
|
|
93
|
|
|
return $this->update($user, $comment); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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.