Completed
Push — master ( c26f0f...c7af1e )
by Konstantinos
18:52
created

PermissionModel::canBeCreatedBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file contains an abstract model class for models that refer to permissions
4
 *
5
 * @package    BZiON\Models
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * A Model that can be managed by users with specific permissions
11
 * @package BZiON\Models
12
 */
13
abstract class PermissionModel extends Model
14
{
15
    /**
16
     * The permission required to create such a model
17
     * @var string|null
18
     */
19
    const CREATE_PERMISSION = null;
20
21
    /**
22
     * The permission required to edit such a model
23
     * @var string|null
24
     */
25
    const EDIT_PERMISSION = null;
26
27
    /**
28
     * The permission required to mark this model as deleted
29
     * @var string|null
30
     */
31
    const SOFT_DELETE_PERMISSION = null;
32
33
    /**
34
     * The permission required to delete this model from the database
35
     * @var string|null
36
     */
37
    const HARD_DELETE_PERMISSION = null;
38
39
    /**
40
     * Find out whether a player should know that a model exists
41
     *
42
     * @param  Player  $player      The player in question
43
     * @param  bool $showDeleted false to hide deleted models even from admins
44
     * @return bool
45
     */
46
    public function canBeSeenBy($player, $showDeleted = false)
47
    {
48
        if ($this->isDeleted()) {
49
            if (!$showDeleted) {
50
                return false;
51
            }
52
53
            // Only admins can see deleted models
54
            return $this->canBeHardDeletedBy($player);
55
        }
56
57
        if (!$this->isActive()) {
58
            // Only admins can see hidden models
59
            return $this->canBeEditedBy($player);
60
        }
61
62
        return true;
63
    }
64
65
    /**
66
     * Find out whether a player can create a model of this type
67
     *
68
     * If possible, prefer to override PermissionModel::CREATE_PERMISSION
69
     *
70
     * @param  Player $player
71
     * @return bool
72
     */
73
    public static function canBeCreatedBy($player)
74
    {
75
        return $player->hasPermission(static::CREATE_PERMISSION);
76
    }
77
78
    /**
79
     * Find out whether a player can edit this model
80
     *
81
     * If possible, prefer to override PermissionModel::EDIT_PERMISSION and/or
82
     * PermissionModel::isEditor()
83
     *
84
     * @param  Player  $player
85
     * @return bool
86
     */
87
    public function canBeEditedBy($player)
88
    {
89
        return $player->hasPermission(static::EDIT_PERMISSION) || $this->isEditor($player);
90
    }
91
92
    /**
93
     * Find out whether a player can soft delete the model
94
     *
95
     * If possible, prefer to override PermissionModel::SOFT_DELETE_PERMISSION
96
     * and/or PermissionModel::isEditor()
97
     *
98
     * @param  Player  $player
99
     * @return bool
100
     */
101
    public function canBeSoftDeletedBy($player)
102
    {
103
        return $player->hasPermission(static::SOFT_DELETE_PERMISSION) || $this->isEditor($player);
104
    }
105
106
    /**
107
     * Find out whether a player can delete this model
108
     *
109
     * If possible, prefer to override PermissionModel::HARD_DELETE_PERMISSION
110
     *
111
     * @param  Player  $player
112
     * @return bool
113
     */
114
    public function canBeHardDeletedBy($player)
115
    {
116
        return $player->hasPermission(static::HARD_DELETE_PERMISSION);
117
    }
118
119
    /**
120
     * Find out whether a player can edit or delete the model even without
121
     * having the appropriate permissions (for example, a team owner should be
122
     * able to edit their team)
123
     *
124
     * @param  Player $player
125
     * @return bool
126
     */
127
    protected function isEditor($player)
128
    {
129
        return false;
130
    }
131
}
132