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\Foundation\Security; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Role Definitions. |
16
|
|
|
*/ |
17
|
|
|
class Role |
18
|
|
|
{ |
19
|
|
|
const APP_ADMIN = 'app-admin'; |
20
|
|
|
const APP_MANAGER = 'app-manager'; |
21
|
|
|
const APP_USER = 'app-user'; |
22
|
|
|
const APP_PUBLIC = 'app-public'; |
23
|
|
|
|
24
|
|
|
const PROJECT_MANAGER = 'project-manager'; |
25
|
|
|
const PROJECT_MEMBER = 'project-member'; |
26
|
|
|
const PROJECT_VIEWER = 'project-viewer'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get application roles. |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function getApplicationRoles() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
self::APP_ADMIN => t('Administrator'), |
37
|
|
|
self::APP_MANAGER => t('Manager'), |
38
|
|
|
self::APP_USER => t('User'), |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get project roles. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function getProjectRoles() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
self::PROJECT_MANAGER => t('Project Manager'), |
51
|
|
|
self::PROJECT_MEMBER => t('Project Member'), |
52
|
|
|
self::PROJECT_VIEWER => t('Project Viewer'), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Check if the given role is custom or not. |
58
|
|
|
* |
59
|
|
|
* @param string $role |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function isCustomProjectRole($role) |
64
|
|
|
{ |
65
|
|
|
return !empty($role) && $role !== self::PROJECT_MANAGER && $role !== self::PROJECT_MEMBER && $role !== self::PROJECT_VIEWER; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get role name. |
70
|
|
|
* |
71
|
|
|
* @param string $role |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getRoleName($role) |
76
|
|
|
{ |
77
|
|
|
$roles = $this->getApplicationRoles() + $this->getProjectRoles(); |
78
|
|
|
|
79
|
|
|
return isset($roles[$role]) ? $roles[$role] : t('Unknown'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.