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 Tinyissue\Contracts\Model\AccessControl; |
15
|
|
|
use Tinyissue\Model\User; |
16
|
|
|
use Tinyissue\Model\Permission; |
17
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
18
|
|
|
use Tinyissue\Model\Project; |
19
|
|
|
use Tinyissue\Model\Traits\Project\Issue\Attachment\CrudTrait; |
20
|
|
|
use Tinyissue\Model\Traits\Project\Issue\Attachment\RelationTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Attachment is model class for project attachments. |
24
|
|
|
* |
25
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
26
|
|
|
* |
27
|
|
|
* @property int $id |
28
|
|
|
* @property int $uploaded_by |
29
|
|
|
* @property int $issue_id |
30
|
|
|
* @property int $comment_id |
31
|
|
|
* @property string $filename |
32
|
|
|
* @property string $fileextension |
33
|
|
|
* @property int $filesize |
34
|
|
|
* @property string $upload_token |
35
|
|
|
* @property Project\Issue $issue |
36
|
|
|
* @property User $user |
37
|
|
|
* @property Comment $comment |
38
|
|
|
*/ |
39
|
|
|
class Attachment extends BaseModel implements AccessControl |
40
|
|
|
{ |
41
|
|
|
use CrudTrait, |
42
|
|
|
RelationTrait; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Timestamp enabled. |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
public $timestamps = true; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Name of database table. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $table = 'projects_issues_attachments'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* List of allowed columns to be used in $this->fill(). |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
protected $fillable = [ |
64
|
|
|
'uploaded_by', |
65
|
|
|
'filename', |
66
|
|
|
'fileextension', |
67
|
|
|
'filesize', |
68
|
|
|
'upload_token', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Whether or not the file extension is supported image type. |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
3 |
|
public function isImage() |
77
|
|
|
{ |
78
|
3 |
|
return in_array($this->fileextension, [ |
79
|
3 |
|
'jpg', |
80
|
|
|
'jpeg', |
81
|
|
|
'JPG', |
82
|
|
|
'JPEG', |
83
|
|
|
'png', |
84
|
|
|
'PNG', |
85
|
|
|
'gif', |
86
|
|
|
'GIF', |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Url to attachment download. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
3 |
|
public function download() |
96
|
|
|
{ |
97
|
3 |
|
return \URL::to('project/' . $this->issue->project_id . '/issue/' . $this->issue_id . '/download/' . $this->id); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Url to display attachment. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function display() |
106
|
|
|
{ |
107
|
|
|
return \URL::to('project/' . $this->issue->project_id . '/issue/' . $this->issue_id . '/display/' . $this->id); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Generate a URL to delete attachment. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
2 |
|
public function toDelete() |
116
|
|
|
{ |
117
|
2 |
|
return \URL::to('project/' . $this->issue->project_id . '/issue/' . $this->issue_id . '/delete/' . $this->id); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Whether a user can view the issue. |
122
|
|
|
* |
123
|
|
|
* @param User $user |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function canView(User $user) |
128
|
|
|
{ |
129
|
|
|
return $this->issue->canView($user); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Whether a user can edit the attachment. |
134
|
|
|
* |
135
|
|
|
* @param User $user |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
2 |
|
public function canEdit(User $user) |
140
|
|
|
{ |
141
|
2 |
|
return $user->id === $this->uploaded_by || ($this->canView($user) && $user->permission(Permission::PERM_ISSUE_MODIFY)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $permission |
146
|
|
|
* @param User $user |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function can($permission, User $user) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$editPermissions = [ |
153
|
|
|
Permission::PERM_ISSUE_CREATE, |
154
|
|
|
Permission::PERM_ISSUE_MODIFY, |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
if (in_array($permission, $editPermissions)) { |
158
|
|
|
return $this->canEdit($user); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->canView($user); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.