ProjectController.php
3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace App\Controllers;
use App\Constants\AclRoles;
use App\Model\Project;
use App\Model\User;
use App\Model\UserProject;
use PhalconApi\Constants\ErrorCodes;
use PhalconApi\Exception;
use PhalconRest\Mvc\Controllers\CrudResourceController;
class ProjectController extends CrudResourceController
{
/**
* Возвращает все проэкты залогиненого пользователя
*
* @return mixed
* @throws Exception
*/
public function allAction()
{
$user_id = $this->userService->getIdentity();
$projects = Project::find(["user_id = '$user_id'"]);
if (count($projects) == 0)
{
$projects = User::findFirst([$user_id])->projects;
if (count($projects) == 0)
{
throw new Exception(ErrorCodes::GENERAL_NOT_FOUND, 'Projects not found');
}
else
{
var_dump($projects);
return $this->createArrayResponse($projects, 'projects');
}
}
else
{
return $this->createArrayResponse($projects, 'projects');
}
}
/**
* @param $data
* @param $isUpdate
* @return bool
* @throws Exception
*/
public function postDataValid($data, $isUpdate)
{
if ($isUpdate)
{
if (isset($data['user_id']))
{
throw new Exception(ErrorCodes::ACCESS_DENIED, 'Find user_id value in post-data. Operation is not allowed.');
}
return true;
}
else
{
if (isset($data['user_id']))
{
throw new Exception(ErrorCodes::ACCESS_DENIED, 'Find user_id value in post-data. Operation is not allowed.');
}
return true;
}
}
/**
*
* @param Project $item
*/
protected function beforeCreate(Project $item)
{
$user_id = $this->userService->getIdentity();
$item->user_id = $user_id;
}
/**
* @param Project $item
* @return bool|void
*/
protected function afterCreate(Project $item)
{
$data = ['project_id' => $item->id, 'user_id' => $item->user_id];
$junction = new UserProject();
$junction->user_id = $item->user_id;
$junction->project_id = $item->id;
if (!$junction->save())
{
return $this->onCreateFailed($junction, $data);
}
return true;
}
/**
* @param $id
* @throws Exception
*/
protected function beforeHandleUpdate($id)
{
$user_id = $this->userService->getIdentity();
if(!UserProject::findFirst(["user_id = '$user_id'"]) && $this->userService->getRole() !== AclRoles::ADMINISTRATOR)
{
throw new Exception(ErrorCodes::ACCESS_DENIED, 'Operation is not allowed');
}
}
/**
* @param $id
* @throws Exception
*/
protected function beforeHandleRemove($id)
{
$project = Project::findFirst($id)->id;
if (empty($project))
{
throw new Exception(ErrorCodes::GENERAL_NOT_FOUND, 'Project with `id`='.$id.' not found');
}
$user_id = $this->userService->getIdentity();
$project = Project::findFirst(["user_id = '$user_id' AND id = '$id'"]);
if (empty($project) && $this->userService->getRole() !== AclRoles::ADMINISTRATOR)
{
throw new Exception(ErrorCodes::ACCESS_DENIED, 'Operation is not allowed');
}
else
{
$project_links = UserProject::find(["project_id = '$id'"]);
foreach ($project_links as $project_link) {
$this->removeItem($project_link);
}
}
}
}