UserController.php
7.61 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Created by PhpStorm.
* User: Vitaliy
* Date: 12.06.14
* Time: 12:02
*/
namespace controllers;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class UserController extends \Phalcon\Mvc\Controller
{
public function initialize()
{
$this->view->setTemplateAfter('common');
}
public function registrationAction()
{
$error = false;
$user['name'] = $this->request->getPost('name', 'string');
$user['email'] = $this->request->getPost('email', 'email');
$user['status'] = 'New User';
$user['password'] = $this->request->getPost('password', 'string');
$user['confirm_password'] = $this->request->getPost('confirm_password', 'string');
$check = \users::findFirst(array("email = '{$user['email']}'"));
if($check instanceof \users) {
echo "Такой e-mail уже существует";
$error = true;
}
if($user['password'] != $user['confirm_password']) {
echo "Неверно введен пароль";
$error = true;
} else {
$user['password'] = $this->common->hashPasswd( $user['password'] );
}
$model = new \users;
if(!$error && $model->save($user)) {
echo "Вы успешно зарегестрированны";
} else {
echo "Произошла ошибка регистрации";
}
}
public function loginAction()
{
$email = $this->request->getPost('email', 'email');
$password = $this->request->getPost('password', 'string');
$password = $this->common->hashPasswd( $password );
$user_model = new \users();
$model = $user_model::findFirst(array(
"email = '$email'",
"password => '$password'",
));
if($model instanceof \users) {
$Online = date('Y-m-d H:i:s');
$model->last_online = $Online;
$model->save();
$this->session->set("user-name", $model->name);
$this->session->set("user-status", $model->status);
$this->session->set("user-id", $model->id);
$this->session->set("project-id", $model->issetProjects(true));
$this->response->redirect('index');
} else {
echo "Пользователя с такими данными не существует";
}
}
public function logoutAction()
{
$this->session->destroy();
return $this->response->redirect('index');
}
public function checkAction() {
$data = $this->request->getPost('data');
$data = json_decode($data);
if(isset($data->id)) {
$model = \users::findFirst(array("$data->name= '$data->value' AND id = '$data->id'"));
if($model instanceof \users) {
$result = array(
'errors' => false,
'message' => ""
);
$data = json_encode($result);
$this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
echo $data;
die();
}
}
$model = \users::findFirst("{$data->name}= '{$data->value}'");
if($model instanceof \users) {
$result = array(
'errors' => true,
'message' => "$data->value уже занято"
);
} else {
$result = array(
'errors' => false,
'message' => "$data->value свободно"
);
}
$data = json_encode($result);
$this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
echo $data;
}
public function chosenProjectAction()
{
$this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
try{
$id = $this->request->getPost('id', 'int', NULL);
if($id) {
$project_model = new \users();
$projectsData = $project_model->issetProjects();
foreach($projectsData as $data){
if($data->toArray()['project_id'] == $id) {
$this->session->set("project-id", $id);
break;
}
}
} else {
throw new \Exception();
}
}
catch(\Exception $e){
$this->flash->error( 'При выборе проекта произошла ошибка' );
}
}
public function accountAction(){
$id = $this->session->get("user-id");
if( $this->request->isPost('update') )
{
$user['name'] = $this->request->getPost('name', 'string');
$user['email'] = $this->request->getPost('email', 'email');
$user['status'] = $this->request->getPost('status', 'string');
$role = $this->request->getPost('role');
$projects = $this->request->getPost('projects');
$new_password = $this->request->getPost('new_password', 'string');
$new_password_r = $this->request->getPost('new_password_r', 'string');
$model = \users::findFirst(array("id = '$id'"));
if(!$model instanceof \users) {
echo "Ползователь не найден";
$this->error= true;
}
if($model->email != $user['email']) {
$check = \users::findFirst(array("email = '{$user['email']}'"));
if($check instanceof \users) {
echo "Такой e-mail уже существует";
$this->error= true;
}
}
if(!$this->error && !empty($new_password)){
if($new_password != $new_password_r) {
$this->error= true;
echo "Неверно указан проверочный пароль";
} else {
$user['password'] = $this->common->hashPasswd($new_password);
}
}
if(!$this->error) {
$model->save($user);
$model->userToRole->delete();
foreach($role as $item) {
$userToRole = new \userToServices;
$userToRole->user_id = $model->id;
$userToRole->role_id = $item;
$userToRole->save();
}
$model->userToProjects->delete();
foreach($projects as $project) {
$userToProjects = new \userToProjects;
$userToProjects->user_id = $model->id;
$userToProjects->project_id = $project;
$userToProjects->save();
}
return $this->response->redirect('user_index');
}
}
if(!$id) {
echo "Не указан номер пользователя";
} else {
$data = \users::findFirst("id = '$id'");
if($data instanceof \users) {
$role = \usersRole::find();
$status = \usersStatus::find();
$projects = \projects::find(array("order" => 'id'));
$this->view->setVars([
'role' => $role,
'status' => $status,
'projects' => $projects,
'data' => $data
]);
}
}
}
}