273 lines
12 KiB
PHP
273 lines
12 KiB
PHP
<?php
|
|
namespace App\Controller\Manager;
|
|
use Cake\Event\Event;
|
|
use Cake\ORM\TableRegistry;
|
|
use Cake\Auth\DefaultPasswordHasher;
|
|
use App\Controller\AppController;
|
|
|
|
class UsersController extends AppController {
|
|
|
|
public function beforeFilter(Event $event) {
|
|
parent::beforeFilter($event);
|
|
$this->viewBuilder()->setLayout('manager');
|
|
|
|
$this->set('isAuthorized', $this->isAuthorized());
|
|
}
|
|
|
|
public function beforeRender(Event $event)
|
|
{
|
|
$this->Auth->allow(['*']);
|
|
}
|
|
|
|
public function login() {
|
|
if ($this->isAuthorized()) {
|
|
$this->redirect([
|
|
'controller' => 'Managers',
|
|
'action' => 'index',
|
|
'prefix' => 'manager'
|
|
]);
|
|
} else {
|
|
$this->viewBuilder()->setLayout('manager_login');
|
|
|
|
$jsConfig = $this->getAppJsConfig("", false);
|
|
|
|
if ($this->request->is('post')) {
|
|
$data = $this->request->getData();
|
|
$jsConfig["defaultLoginApp"] = $data['defaultLoginApp'] = str_replace("string:", "", $data['defaultLoginApp']);
|
|
|
|
$user = $this->Auth->identify();
|
|
$usersTable = TableRegistry::getTableLocator()->get('Users');
|
|
|
|
if ($user) {
|
|
if (!empty($user['is_active'])) {
|
|
$user["available_apps"] = @json_decode($user["available_apps"], true);
|
|
$this->Auth->setUser($user);
|
|
|
|
$usersTable->updateAll([
|
|
'logging_attempts' => 0
|
|
],[
|
|
'id' => $user['id']
|
|
]);
|
|
|
|
if (!empty($data['defaultLoginApp']) && in_array($data['defaultLoginApp'], $user["available_apps"])) {
|
|
$this->request->getSession()->write('currentApp', $data['defaultLoginApp']);
|
|
|
|
return $this->redirect($this->config["appsList"][$data['defaultLoginApp']]["adminUrl"]);
|
|
} else {
|
|
$this->request->getSession()->write('currentApp', "cut");
|
|
return $this->redirect($this->Auth->redirectUrl());
|
|
}
|
|
} else {
|
|
$this->Flash->error('Ваш аккаунт неактивен.<br/>Обратитесь к администратору для активации.');
|
|
}
|
|
} else {
|
|
$existsUsername = $usersTable->find()->where([
|
|
'username' => $data['username']
|
|
])->first();
|
|
|
|
if (!empty($existsUsername)) {
|
|
$existsUsername->logging_attempts = $existsUsername->logging_attempts + 1;
|
|
if ($existsUsername->logging_attempts >= 5) {
|
|
$existsUsername->is_active = false;
|
|
}
|
|
|
|
$usersTable->save($existsUsername);
|
|
}
|
|
|
|
$this->Flash->error('Неверное имя пользователя или пароль.');
|
|
}
|
|
} else {
|
|
$jsConfig["defaultLoginApp"] = "cut";
|
|
}
|
|
|
|
$this->set('jsConfig', $jsConfig);
|
|
}
|
|
}
|
|
|
|
public function logout() {
|
|
$this->request->getSession()->delete('currentApp');
|
|
return $this->redirect($this->Auth->logout());
|
|
}
|
|
|
|
|
|
public function index() {
|
|
|
|
}
|
|
|
|
public function usersList() {
|
|
$this->checkRegisteredAsAdmin();
|
|
$this->set('jsConfig', $this->getAppJsConfig("", true));
|
|
|
|
}
|
|
|
|
function addUpdateUser() {
|
|
$result = [
|
|
'status' => 'success'
|
|
];
|
|
|
|
if ($this->isAuthorized(['admin'])) {
|
|
$data = $this->request->getData();
|
|
if (!empty($data)) {
|
|
$usersTable = TableRegistry::getTableLocator()->get('Users');
|
|
if (!empty($data['password']) && ($data['password'] != $data['passwordRepeat'])) {
|
|
$result = [
|
|
'status' => 'warning',
|
|
'warning' => 'Пароль и подтверждение пароля не совпадают'
|
|
];
|
|
} else {
|
|
$isOk = false;
|
|
unset($data['last_active']);
|
|
|
|
if (empty($data['id'])) { //add
|
|
$similarUsernameUser = $usersTable->find()->where(['username' => $data['username']])->first();
|
|
if (empty($similarUsernameUser)) {
|
|
$newUser = $usersTable->newEntity();
|
|
$data["available_apps"] = json_encode($data["available_apps"]);
|
|
$newUser = $usersTable->patchEntity($newUser, $data);
|
|
|
|
if ($usersTable->save($newUser)) {
|
|
$isOk = true;
|
|
}
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'warning' => 'Такое имя пользователя/email уже зарегистрированы'
|
|
];
|
|
}
|
|
} else { //update
|
|
$currentUser = $usersTable->find()->where(['id' => $data['id']])->first();
|
|
if (!empty($currentUser)) {
|
|
$similarUsernameUser = $usersTable->find()->where(['username' => $data['username'], 'id !=' => $data['id']])->first();
|
|
if (empty($similarUsernameUser)) {
|
|
if ($data['id'] == $this->Auth->user('id')) {
|
|
unset($data['role']);
|
|
unset($data['is_active']);
|
|
|
|
if (!empty($data['password']) && empty($data['currentPassword'])) {
|
|
$result = [
|
|
'status' => 'error',
|
|
'error' => 'Необходимо указать текущий пароль'
|
|
];
|
|
}
|
|
|
|
if (!empty($data['password']) && !empty($data['currentPassword'])) {
|
|
if (!(new DefaultPasswordHasher)->check($data['currentPassword'], $currentUser->password)) {
|
|
$result = [
|
|
'status' => 'error',
|
|
'error' => 'Неверно указан текущий пароль'
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($data['is_active']) && empty($currentUser['is_active'])) {
|
|
$data['logging_attempts'] = 0;
|
|
}
|
|
|
|
if ($result['status'] == "success") {
|
|
$data["available_apps"] = json_encode($data["available_apps"]);
|
|
$usersTable->patchEntity($currentUser, $data);
|
|
if ($usersTable->save($currentUser)) {
|
|
$isOk = true;
|
|
}
|
|
}
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'warning' => 'Такое имя пользователя/email уже зарегистрированы'
|
|
];
|
|
}
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'warning' => 'Пользователь не найден'
|
|
];
|
|
}
|
|
|
|
}
|
|
|
|
if ($isOk) {
|
|
if (!empty($data['availableToOrderDefault'])) {
|
|
$usersTable->updateAll(
|
|
['availableToOrderDefault' => false],
|
|
['id !=' => $currentUser->id]
|
|
);
|
|
}
|
|
|
|
$result['users'] = $this->getUsersListForJS();
|
|
$updates = TableRegistry::getTableLocator()->get('Settings')->refreshSettingsUpdateDate("", 'users', $this->Auth->user('id'));
|
|
$result = array_merge($result, $updates);
|
|
}
|
|
}
|
|
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'warning' => 'Недостаточно прав для добавление пользователя'
|
|
];
|
|
|
|
}
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'warning' => 'Недостаточно прав для добавление пользователя'
|
|
];
|
|
}
|
|
|
|
echo $this->jsonSimpleResponse($result);
|
|
}
|
|
|
|
|
|
function delete() {
|
|
$result = [
|
|
'status' => 'success'
|
|
];
|
|
|
|
if ($this->isAuthorized(['admin'])) {
|
|
$data = $this->request->getData();
|
|
if (!empty($data['id'])) {
|
|
$orderCut = TableRegistry::getTableLocator()->get('CutOrders')->find()->where(['manager_id' => $data['id'], 'order_type' => 'order'])->count();
|
|
$orderCalcDoors = TableRegistry::getTableLocator()->get('CalcDoorsOrders')->find()->where(['manager_id' => $data['id'], 'order_type' => 'order'])->count();
|
|
$orderFasad = TableRegistry::getTableLocator()->get('FasadOrders')->find()->where(['manager_id' => $data['id'], 'order_type' => 'order'])->count();
|
|
|
|
if (empty($orderCut) && empty($orderCalcDoors) && empty($orderFasad)) {
|
|
$usersTable = TableRegistry::getTableLocator()->get('Users');
|
|
$user = $usersTable->find()->where(['id' => $data['id']])->first();
|
|
if (!empty($user)) {
|
|
if ($data['id'] == $this->Auth->user('id')) {
|
|
$result = [
|
|
'status' => 'error',
|
|
'error' => 'Вы не можете удалить свой аккаунт'
|
|
];
|
|
} else {
|
|
$usersTable->delete($user);
|
|
|
|
$updates = TableRegistry::getTableLocator()->get('Settings')->refreshSettingsUpdateDate("", 'users', $this->Auth->user('id'));
|
|
$result = array_merge($result, $updates);
|
|
}
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'error' => 'Пользователь не найден'
|
|
];
|
|
}
|
|
} else {
|
|
$result = [
|
|
'status' => 'warning',
|
|
'warning' => 'Удаление пользователя невозможно, так как у него есть заказы на исполнении.'
|
|
];
|
|
}
|
|
}
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'warning' => 'Недостаточно прав для удаления данного пользователя'
|
|
];
|
|
}
|
|
|
|
echo $this->jsonSimpleResponse($result);
|
|
}
|
|
|
|
|
|
}
|