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\Http\Controllers\Project; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Exceptions\AccessForbiddenException; |
15
|
|
|
use Jitamin\Http\Controllers\Controller; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ProjectRoleController. |
19
|
|
|
*/ |
20
|
|
|
class ProjectRoleController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Show roles and permissions. |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function show() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$project = $this->getProject(); |
28
|
|
|
|
29
|
|
|
$this->response->html($this->helper->layout->project('project/role/show', [ |
|
|
|
|
30
|
|
|
'project' => $project, |
31
|
|
|
'roles' => $this->projectRoleModel->getAllWithRestrictions($project['id']), |
|
|
|
|
32
|
|
|
'title' => t('Custom Project Roles'), |
33
|
|
|
])); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Show form to create new role. |
38
|
|
|
* |
39
|
|
|
* @param array $values |
40
|
|
|
* @param array $errors |
41
|
|
|
* |
42
|
|
|
* @throws AccessForbiddenException |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function create(array $values = [], array $errors = []) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$project = $this->getProject(); |
47
|
|
|
|
48
|
|
|
$this->response->html($this->template->render('project/role/create', [ |
|
|
|
|
49
|
|
|
'project' => $project, |
50
|
|
|
'values' => $values + ['project_id' => $project['id']], |
51
|
|
|
'errors' => $errors, |
52
|
|
|
])); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Save new role. |
57
|
|
|
*/ |
58
|
|
|
public function store() |
59
|
|
|
{ |
60
|
|
|
$project = $this->getProject(); |
61
|
|
|
$values = $this->request->getValues(); |
|
|
|
|
62
|
|
|
|
63
|
|
|
list($valid, $errors) = $this->projectRoleValidator->validateCreation($values); |
|
|
|
|
64
|
|
|
|
65
|
|
|
if ($valid) { |
66
|
|
|
$role_id = $this->projectRoleModel->create($project['id'], $values['role']); |
|
|
|
|
67
|
|
|
|
68
|
|
|
if ($role_id !== false) { |
69
|
|
|
$this->flash->success(t('Your custom project role has been created successfully.')); |
|
|
|
|
70
|
|
|
} else { |
71
|
|
|
$this->flash->failure(t('Unable to create custom project role.')); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->response->redirect($this->helper->url->to('Project/ProjectRoleController', 'show', ['project_id' => $project['id']])); |
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
$this->create($values, $errors); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Show form to change existing role. |
82
|
|
|
* |
83
|
|
|
* @param array $values |
84
|
|
|
* @param array $errors |
85
|
|
|
* |
86
|
|
|
* @throws AccessForbiddenException |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function edit(array $values = [], array $errors = []) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$project = $this->getProject(); |
91
|
|
|
|
92
|
|
|
if (empty($values)) { |
93
|
|
|
$values = $role; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->response->html($this->template->render('project/role/edit', [ |
|
|
|
|
97
|
|
|
'role' => $this->projectRoleModel->getById($project['id'], $this->request->getIntegerParam('role_id')), |
|
|
|
|
98
|
|
|
'project' => $project, |
99
|
|
|
'values' => $values, |
100
|
|
|
'errors' => $errors, |
101
|
|
|
])); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Update role. |
106
|
|
|
*/ |
107
|
|
|
public function update() |
108
|
|
|
{ |
109
|
|
|
$project = $this->getProject(); |
110
|
|
|
$role_id = $this->request->getIntegerParam('role_id'); |
|
|
|
|
111
|
|
|
$role = $this->projectRoleModel->getById($project['id'], $role_id); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$values = $this->request->getValues(); |
|
|
|
|
114
|
|
|
|
115
|
|
|
list($valid, $errors) = $this->projectRoleValidator->validateModification($values); |
|
|
|
|
116
|
|
|
|
117
|
|
|
if ($valid) { |
118
|
|
|
if ($this->projectRoleModel->update($role['role_id'], $project['id'], $values['role'])) { |
|
|
|
|
119
|
|
|
$this->flash->success(t('Your custom project role has been updated successfully.')); |
|
|
|
|
120
|
|
|
} else { |
121
|
|
|
$this->flash->failure(t('Unable to update custom project role.')); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->response->redirect($this->helper->url->to('Project/ProjectRoleController', 'show', ['project_id' => $project['id']])); |
|
|
|
|
125
|
|
|
} else { |
126
|
|
|
$this->edit($values, $errors); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Remove a custom role. |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function remove() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$project = $this->getProject(); |
136
|
|
|
$role_id = $this->request->getIntegerParam('role_id'); |
|
|
|
|
137
|
|
|
|
138
|
|
|
if ($this->request->isPost()) { |
|
|
|
|
139
|
|
|
$this->request->checkCSRFToken(); |
|
|
|
|
140
|
|
|
if ($this->projectRoleModel->remove($project['id'], $role_id)) { |
|
|
|
|
141
|
|
|
$this->flash->success(t('Custom project role removed successfully.')); |
|
|
|
|
142
|
|
|
} else { |
143
|
|
|
$this->flash->failure(t('Unable to remove this project role.')); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->response->redirect($this->helper->url->to('Project/ProjectRoleController', 'show', ['project_id' => $project['id']])); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$role = $this->projectRoleModel->getById($project['id'], $role_id); |
|
|
|
|
150
|
|
|
|
151
|
|
|
return $this->response->html($this->helper->layout->project('project/role/remove', [ |
|
|
|
|
152
|
|
|
'project' => $project, |
153
|
|
|
'role' => $role, |
154
|
|
|
])); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.