ProjectPermissionModel   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 168
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryByRole() 0 22 2
A findUsernames() 0 20 1
A isEverybodyAllowed() 0 8 1
A isUserAllowed() 0 11 2
A isAssignable() 0 10 3
A isMember() 0 4 1
A getActiveProjectIds() 0 4 1
A getProjectIds() 0 4 1
A duplicate() 0 5 2
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
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 Jitamin\Model;
13
14
use Jitamin\Filter\ProjectGroupRoleProjectFilter;
15
use Jitamin\Filter\ProjectGroupRoleUsernameFilter;
16
use Jitamin\Filter\ProjectUserRoleProjectFilter;
17
use Jitamin\Filter\ProjectUserRoleUsernameFilter;
18
use Jitamin\Foundation\Database\Model;
19
use Jitamin\Foundation\Security\Role;
20
21
/**
22
 * Project Permission.
23
 */
24
class ProjectPermissionModel extends Model
25
{
26
    /**
27
     * Get query for project users overview.
28
     *
29
     * @param array  $project_ids
30
     * @param string $role
31
     *
32
     * @return \PicoDb\Table
33
     */
34
    public function getQueryByRole(array $project_ids, $role)
35
    {
36
        if (empty($project_ids)) {
37
            $project_ids = [-1];
38
        }
39
40
        return $this
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
41
            ->db
42
            ->table(ProjectUserRoleModel::TABLE)
43
            ->join(UserModel::TABLE, 'id', 'user_id')
44
            ->join(ProjectModel::TABLE, 'id', 'project_id')
45
            ->eq(ProjectUserRoleModel::TABLE.'.role', $role)
46
            ->eq(ProjectModel::TABLE.'.is_private', 0)
47
            ->in(ProjectModel::TABLE.'.id', $project_ids)
48
            ->columns(
49
                UserModel::TABLE.'.id',
50
                UserModel::TABLE.'.username',
51
                UserModel::TABLE.'.name',
52
                ProjectModel::TABLE.'.name AS project_name',
53
                ProjectModel::TABLE.'.id'
54
            );
55
    }
56
57
    /**
58
     * Get all usernames (fetch users from groups).
59
     *
60
     * @param int    $project_id
61
     * @param string $input
62
     *
63
     * @return array
64
     */
65
    public function findUsernames($project_id, $input)
66
    {
67
        $userMembers = $this->projectUserRoleQuery
0 ignored issues
show
Documentation introduced by
The property projectUserRoleQuery does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
68
            ->withFilter(new ProjectUserRoleProjectFilter($project_id))
69
            ->withFilter(new ProjectUserRoleUsernameFilter($input))
70
            ->getQuery()
71
            ->findAllByColumn('username');
72
73
        $groupMembers = $this->projectGroupRoleQuery
0 ignored issues
show
Documentation introduced by
The property projectGroupRoleQuery does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
74
            ->withFilter(new ProjectGroupRoleProjectFilter($project_id))
75
            ->withFilter(new ProjectGroupRoleUsernameFilter($input))
76
            ->getQuery()
77
            ->findAllByColumn('username');
78
79
        $members = array_unique(array_merge($userMembers, $groupMembers));
80
81
        sort($members);
82
83
        return $members;
84
    }
85
86
    /**
87
     * Return true if everybody is allowed for the project.
88
     *
89
     * @param int $project_id Project id
90
     *
91
     * @return bool
92
     */
93
    public function isEverybodyAllowed($project_id)
94
    {
95
        return $this->db
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
96
                    ->table(ProjectModel::TABLE)
97
                    ->eq('id', $project_id)
98
                    ->eq('is_everybody_allowed', 1)
99
                    ->exists();
100
    }
101
102
    /**
103
     * Return true if the user is allowed to access a project.
104
     *
105
     * @param int $project_id
106
     * @param int $user_id
107
     *
108
     * @return bool
109
     */
110
    public function isUserAllowed($project_id, $user_id)
111
    {
112
        if ($this->userSession->isAdmin()) {
0 ignored issues
show
Documentation introduced by
The property userSession does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
113
            return true;
114
        }
115
116
        return in_array(
117
            $this->projectUserRoleModel->getUserRole($project_id, $user_id),
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
118
            [Role::PROJECT_MANAGER, Role::PROJECT_MEMBER, Role::PROJECT_VIEWER]
119
        );
120
    }
121
122
    /**
123
     * Return true if the user is assignable.
124
     *
125
     * @param int $project_id
126
     * @param int $user_id
127
     *
128
     * @return bool
129
     */
130
    public function isAssignable($project_id, $user_id)
131
    {
132
        if ($this->userModel->isActive($user_id)) {
0 ignored issues
show
Documentation introduced by
The property userModel does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
133
            $role = $this->projectUserRoleModel->getUserRole($project_id, $user_id);
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
134
135
            return !empty($role) && $role !== Role::PROJECT_VIEWER;
136
        }
137
138
        return false;
139
    }
140
141
    /**
142
     * Return true if the user is member.
143
     *
144
     * @param int $project_id
145
     * @param int $user_id
146
     *
147
     * @return bool
148
     */
149
    public function isMember($project_id, $user_id)
150
    {
151
        return in_array($this->projectUserRoleModel->getUserRole($project_id, $user_id), [Role::PROJECT_MEMBER, Role::PROJECT_MANAGER, Role::PROJECT_VIEWER]);
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
152
    }
153
154
    /**
155
     * Get active project ids by user.
156
     *
157
     * @param int $user_id
158
     *
159
     * @return array
160
     */
161
    public function getActiveProjectIds($user_id)
162
    {
163
        return array_keys($this->projectUserRoleModel->getActiveProjectsByUser($user_id));
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
164
    }
165
166
    /**
167
     * Get all project ids by user.
168
     *
169
     * @param int $user_id
170
     *
171
     * @return array
172
     */
173
    public function getProjectIds($user_id)
174
    {
175
        return array_keys($this->projectUserRoleModel->getProjectsByUser($user_id));
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
176
    }
177
178
    /**
179
     * Copy permissions to another project.
180
     *
181
     * @param int $project_src_id Project Template
182
     * @param int $project_dst_id Project that receives the copy
183
     *
184
     * @return bool
185
     */
186
    public function duplicate($project_src_id, $project_dst_id)
187
    {
188
        return $this->projectUserRoleModel->duplicate($project_src_id, $project_dst_id) &&
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
189
            $this->projectGroupRoleModel->duplicate($project_src_id, $project_dst_id);
0 ignored issues
show
Documentation introduced by
The property projectGroupRoleModel does not exist on object<Jitamin\Model\ProjectPermissionModel>. 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...
190
    }
191
}
192