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\Model\Project\Issue; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Collection; |
15
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
16
|
|
|
use Tinyissue\Contracts\Model\AccessControl; |
17
|
|
|
use Tinyissue\Model\Message\Queue; |
18
|
|
|
use Tinyissue\Model\Permission; |
19
|
|
|
use Tinyissue\Model\Project\Issue; |
20
|
|
|
use Tinyissue\Model\Traits\Project\Issue\Comment\CrudTrait; |
21
|
|
|
use Tinyissue\Model\Traits\Project\Issue\Comment\QueueTrait; |
22
|
|
|
use Tinyissue\Model\Traits\Project\Issue\Comment\RelationTrait; |
23
|
|
|
use Tinyissue\Model\User; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Comment is model class for project issue comments. |
27
|
|
|
* |
28
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
29
|
|
|
* |
30
|
|
|
* @property int $id |
31
|
|
|
* @property int $issue_id |
32
|
|
|
* @property int $project_id |
33
|
|
|
* @property string $comment |
34
|
|
|
* @property int $created_by |
35
|
|
|
* @property User $user |
36
|
|
|
* @property Issue $issue |
37
|
|
|
* @property Collection $attachments |
38
|
|
|
* @property User\Activity $activity |
39
|
|
|
* @property Queue $messagesQueue |
40
|
|
|
*/ |
41
|
|
|
class Comment extends BaseModel implements AccessControl |
42
|
|
|
{ |
43
|
|
|
use CrudTrait, |
44
|
|
|
RelationTrait, |
45
|
|
|
QueueTrait; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Timestamp enabled. |
49
|
|
|
* |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
public $timestamps = true; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Name of database table. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $table = 'projects_issues_comments'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* List of allowed columns to be used in $this->fill(). |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $fillable = [ |
67
|
|
|
'created_by', |
68
|
|
|
'project_id', |
69
|
|
|
'issue_id', |
70
|
|
|
'comment', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Whether a user can view the issue. |
75
|
|
|
* |
76
|
|
|
* @param User $user |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
1 |
|
public function canView(User $user) |
81
|
|
|
{ |
82
|
1 |
|
return $this->issue->canView($user); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Whether a user can edit the comment. |
87
|
|
|
* |
88
|
|
|
* @param User $user |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
8 |
|
public function canEdit(User $user) |
93
|
|
|
{ |
94
|
8 |
|
return $user->id === $this->created_by || ($this->canView($user) && $user->permission(Permission::PERM_ISSUE_MODIFY)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $permission |
99
|
|
|
* @param User $user |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function can($permission, User $user) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$editPermissions = [ |
106
|
|
|
Permission::PERM_ISSUE_COMMENT, |
107
|
|
|
Permission::PERM_ISSUE_MODIFY, |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
if (in_array($permission, $editPermissions)) { |
111
|
|
|
return $this->canEdit($user); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->canView($user); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.