Completed
Push — develop ( f06541...810a8c )
by Mohamed
11:09 queued 05:53
created

User::permissionInContext()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.9256

Importance

Changes 6
Bugs 3 Features 1
Metric Value
c 6
b 3
f 1
dl 0
loc 23
ccs 8
cts 12
cp 0.6667
rs 8.5906
cc 5
eloc 11
nc 3
nop 1
crap 5.9256
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;
13
14
use Illuminate\Auth\Authenticatable;
15
use Illuminate\Auth\Passwords\CanResetPassword;
16
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
17
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
18
use Illuminate\Database\Eloquent;
19
use Illuminate\Database\Eloquent\Model;
20
use Tinyissue\Model\Project\Issue;
21
use Auth as Auth;
22
23
/**
24
 * User is model class for users.
25
 *
26
 * @author Mohamed Alsharaf <[email protected]>
27
 *
28
 * @property int    $id
29
 * @property int    $deleted
30
 * @property int    $role_id
31
 * @property string $language
32
 * @property string $email
33
 * @property string $password
34
 * @property string $firstname
35
 * @property string $lastname
36
 * @property string $fullname
37
 */
38
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
39
{
40
    use Authenticatable,
41
        CanResetPassword,
42
        Traits\User\CountTrait,
43
        Traits\User\RelationTrait,
44
        Traits\User\CrudTrait,
45
        Traits\User\QueryTrait;
46
47
    /**
48
     * User name is private.
49
     *
50
     * @var int
51
     */
52
    const PRIVATE_YES = 1;
53
54
    /**
55
     * User name is public.
56
     *
57
     * @var int
58
     */
59
    const PRIVATE_NO = 0;
60
61
    /**
62
     * User status Deleted.
63
     *
64
     * @var int
65
     */
66
    const DELETED_USERS = 1;
67
68
    /**
69
     * User status not deleted.
70
     *
71
     * @var int
72
     */
73
    const NOT_DELETED_USERS = 0;
74
75
    /**
76
     * The database table used by the model.
77
     *
78
     * @var string
79
     */
80
    protected $table = 'users';
81
82
    /**
83
     * The attributes that are mass assignable.
84
     *
85
     * @var array
86
     */
87
    protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private'];
88
89
    /**
90
     * The attributes excluded from the model's JSON form.
91
     *
92
     * @var array
93
     */
94
    protected $hidden = ['password', 'remember_token'];
95
96
    /**
97
     * Collection of user permissions.
98
     *
99
     * @var Eloquent\Collection
100
     */
101
    protected $permission;
102
103
    /**
104
     * Get available languages from translations folder.
105
     *
106
     * @return array
107
     */
108 54
    public static function getLanguages()
109
    {
110 3
        $languages = [];
111
112 3
        $cdir = scandir(__DIR__ . '/../../resources/lang');
113 3
        foreach ($cdir as $value) {
114 3
            if (!in_array($value, ['.', '..'])) {
115 3
                $languages[$value] = $value;
116 3
            }
117 3
        }
118
119 54
        return $languages;
120
    }
121
122
    /**
123
     * Checks to see if $this user is current user.
124
     *
125
     * @return bool
126
     */
127 3
    public function me()
128
    {
129 3
        return $this->id == \Auth::user()->id;
130
    }
131
132
    /**
133
     * Whether or not the user has a valid permission in current context
134
     * e.g. can access the issue or the project.
135
     *
136
     * @param array $params
137
     *
138
     * @return bool
139
     */
140 39
    public function permissionInContext(array $params)
141
    {
142
        // Can access all projects
143 39
        if ($this->permission(Permission::PERM_PROJECT_ALL)) {
144 35
            return true;
145
        }
146
147 8
        $project = array_get($params, 'project', function () use ($params) {
148
            $issue = array_get($params, 'issue');
149
            if ($issue instanceof Issue) {
150
                return $issue->project;
151
            }
152
153
            return;
154 8
        });
155
156
        // Is member of the project
157 8
        if ($project && !$project->isMember($this->id)) {
158 5
            return false;
159
        }
160
161 7
        return true;
162
    }
163
164
    /**
165
     * Whether or not the user has a permission.
166
     *
167
     * @param string $key
168
     *
169
     * @return bool
170
     */
171 54
    public function permission($key)
172
    {
173 54
        $this->loadPermissions();
174 54
        foreach ($this->permission as $permission) {
175 54
            if ($permission->permission->isEqual($key)) {
176 45
                return true;
177
            }
178 54
        }
179
180 28
        return false;
181
    }
182
183
    /**
184
     * Return user full name with property "fullname".
185
     *
186
     * @return string
187
     */
188 39
    public function getFullNameAttribute()
189
    {
190 39
        if ($this->private && (Auth::guest() || !Auth::user()->permission('administration'))) {
0 ignored issues
show
Documentation introduced by
The property private does not exist on object<Tinyissue\Model\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
191 1
            return trans('tinyissue.anonymous');
192
        }
193
194 39
        return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
195
    }
196
}
197