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

CommentPolicy::view()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
nop 2
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 Illuminate\Auth\Access\HandlesAuthorization;
15
use Illuminate\Support\Facades\Gate;
16
use Tinyissue\Extensions\Policies\ProjectAccess;
17
use Tinyissue\Model\Project;
18
use Tinyissue\Model\Project\Issue\Comment;
19
use Tinyissue\Model\User;
20
21
class CommentPolicy
22
{
23
    use HandlesAuthorization, ProjectAccess;
24
25
    /**
26
     * @param User $user
27
     *
28
     * @return bool
29
     */
30
    public function before(User $user)
31
    {
32
        if ($user instanceof User && ($user->isAdmin() || $user->isManager())) {
33
            return true;
34
        }
35
    }
36
37
    /**
38
     * Determine whether the user can view the comment.
39
     *
40
     * @param User    $user
41
     * @param Comment $comment
42
     *
43
     * @return bool
44
     */
45
    public function view(User $user, Comment $comment)
46
    {
47
        return Gate::forUser($user)->allows('view', [$comment->issue, $comment->issue->project]);
48
    }
49
50
    /**
51
     * Determine whether the user can create comments.
52
     *
53
     * @param User          $user
54
     * @param Project\Issue $issue
55
     *
56
     * @return bool
57
     */
58
    public function create(User $user, Project\Issue $issue)
59
    {
60
        return $issue->isOpen() && $issue->project->isMember($user->id);
61
    }
62
63
    /**
64
     * Determine whether the user can update the comment.
65
     *
66
     * @param User    $user
67
     * @param Comment $comment
68
     *
69
     * @return bool
70
     */
71
    public function update(User $user, Comment $comment)
72
    {
73
        return $user->id === $comment->created_by || ($this->view($user, $comment) && $user->isManagerOrMore());
74
    }
75
76
    /**
77
     * Determine whether the user can delete the comment.
78
     *
79
     * @param User    $user
80
     * @param Comment $comment
81
     *
82
     * @return bool
83
     */
84
    public function delete(User $user, Comment $comment)
85
    {
86
        return $this->update($user, $comment);
87
    }
88
}
89