Completed
Push — develop-3.0 ( 5ab583...f20237 )
by Mohamed
06:33
created

AttachmentPolicy::view()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 4
1
<?php
2
3
namespace Tinyissue\Policies;
4
5
use Illuminate\Auth\Access\HandlesAuthorization;
6
use Illuminate\Support\Facades\Gate;
7
use Tinyissue\Model\Project;
8
use Tinyissue\Model\Project\Issue\Attachment;
9
use Tinyissue\Model\User;
10
11
class AttachmentPolicy
12
{
13
    use HandlesAuthorization;
14
15
    /**
16
     * @param User $user
17
     *
18
     * @return bool
19
     */
20
    public function before(User $user)
21
    {
22
        if ($user instanceof User && ($user->isAdmin() || $user->isManager())) {
23
            return true;
24
        }
25
    }
26
27
    /**
28
     * Determine whether the user can view the attachment.
29
     *
30
     * @param User          $user
31
     * @param Attachment    $attachment
32
     * @param Project\Issue $issue
33
     * @param Project       $project
34
     *
35
     * @return bool
36
     */
37
    public function view(User $user, Attachment $attachment, Project\Issue $issue, Project $project)
38
    {
39
        return Gate::forUser($user)->allows('view', [$issue, $project]) || (int) $attachment->uploaded_by === (int) $user->id;
40
    }
41
42
    /**
43
     * Determine whether the user can create attachments.
44
     *
45
     * @param User    $user
46
     * @param Project $project
47
     *
48
     * @return bool
49
     */
50
    public function create(User $user, Project $project)
51
    {
52
        return $project->isMember($user->id);
53
    }
54
55
    /**
56
     * Determine whether the user can update the attachment.
57
     *
58
     * @param User          $user
59
     * @param Attachment    $attachment
60
     * @param Project\Issue $issue
61
     * @param Project       $project
62
     *
63
     * @return bool
64
     */
65
    public function update(User $user, Attachment $attachment, Project\Issue $issue, Project $project)
66
    {
67
        // Issue locked by read only tag
68
        if ($issue->hasReadOnlyTag($user)) {
69
            return false;
70
        }
71
72
        return $attachment->id > 0 && Gate::forUser($user)->allows('view', [$issue, $project]);
73
    }
74
75
    /**
76
     * Determine whether the user can delete the attachment.
77
     *
78
     * @param User          $user
79
     * @param Attachment    $attachment
80
     * @param Project\Issue $issue
81
     * @param Project       $project
82
     *
83
     * @return bool
84
     */
85
    public function delete(User $user, Attachment $attachment, Project\Issue $issue, Project $project)
86
    {
87
        return $this->update($user, $attachment, $issue, $project);
88
    }
89
}
90