Skip to content

Commit 0e59507

Browse files

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
class actionAuthApiAuthLogin extends cmsAction {
4+
5+
/**
6+
* Блокировка прямого вызова экшена
7+
* обязательное свойство
8+
* @var boolean
9+
*/
10+
public $lock_explicit_call = true;
11+
/**
12+
* Результат запроса
13+
* обязательное свойство
14+
* @var array
15+
*/
16+
public $result;
17+
/**
18+
* Флаг, обязующий проверять параметр sig запроса
19+
* sig привязан к домену сайта и к ip адресу посетителя
20+
* @var boolean
21+
*/
22+
public $check_sig = true;
23+
24+
/**
25+
* Возможные параметры запроса
26+
* с правилами валидации
27+
* Если запрос имеет параметры, необходимо описать их здесь
28+
* Правила валидации параметров задаются по аналогии с полями форм
29+
* @var array
30+
*/
31+
public $request_params = array(
32+
'email' => array(
33+
'default' => '',
34+
'rules' => array(
35+
array('required'),
36+
array('email')
37+
)
38+
),
39+
'password' => array(
40+
'default' => '',
41+
'rules' => array(
42+
array('required')
43+
)
44+
)
45+
);
46+
47+
/**
48+
* Массив ключей для удаления
49+
* @var array
50+
*/
51+
public $unset_fields = array(
52+
'user_info' => array( // название ключа в $this->result
53+
'type' => 'item', // list или item
54+
'unsets' => array( // массив названий ключей для удаления
55+
'password', 'password_salt', 'pass_token', 'date_token'
56+
)
57+
)
58+
);
59+
60+
private $user;
61+
62+
public function validateApiRequest() {
63+
64+
$logged_id = cmsUser::login($this->request->get('email', ''), $this->request->get('password', ''), true);
65+
66+
if(!$logged_id){
67+
return array(
68+
'error_code' => 5
69+
);
70+
}
71+
72+
$this->user = cmsCore::getModel('users')->getUser($logged_id);
73+
74+
if ($this->user['is_admin']) {
75+
76+
cmsUser::logout();
77+
78+
return array('error_code' => 15);
79+
80+
}
81+
82+
$this->user['avatar'] = cmsModel::yamlToArray($this->user['avatar']);
83+
if ($this->user['avatar']){
84+
foreach($this->user['avatar'] as $size => $path){
85+
$this->user['avatar'][$size] = $this->cms_config->upload_host_abs.'/'.$path;
86+
}
87+
}
88+
89+
$this->user['is_online'] = true;
90+
91+
cmsEventsManager::hook('auth_login', $logged_id);
92+
93+
return false;
94+
95+
}
96+
97+
public function run(){
98+
99+
$this->result = array(
100+
'session_name' => session_name(),
101+
'session_id' => session_id(),
102+
'user_id' => $this->user['id'],
103+
'user_info' => $this->user
104+
);
105+
106+
}
107+
108+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
class actionAuthApiAuthRestore extends cmsAction {
4+
5+
/**
6+
* Блокировка прямого вызова экшена
7+
* обязательное свойство
8+
* @var boolean
9+
*/
10+
public $lock_explicit_call = true;
11+
/**
12+
* Результат запроса
13+
* обязательное свойство
14+
* @var array
15+
*/
16+
public $result;
17+
/**
18+
* Флаг, обязующий проверять параметр sig запроса
19+
* sig привязан к домену сайта и к ip адресу посетителя
20+
* @var boolean
21+
*/
22+
public $check_sig = true;
23+
24+
/**
25+
* Возможные параметры запроса
26+
* с правилами валидации
27+
* Если запрос имеет параметры, необходимо описать их здесь
28+
* Правила валидации параметров задаются по аналогии с полями форм
29+
* @var array
30+
*/
31+
public $request_params = array(
32+
'email' => array(
33+
'default' => '',
34+
'rules' => array(
35+
array('required'),
36+
array('email')
37+
)
38+
),
39+
);
40+
41+
private $users_model, $user;
42+
43+
public function validateApiRequest() {
44+
45+
$email = $this->request->get('email', '');
46+
47+
$this->users_model = cmsCore::getModel('users');
48+
49+
$this->user = $this->users_model->getUserByEmail($email);
50+
51+
if (!$this->user) {
52+
return array('error_code' => 113);
53+
}
54+
55+
if ($this->user['is_admin']) {
56+
return array('error_code' => 15);
57+
}
58+
59+
if($this->user['is_locked']) {
60+
61+
return array('request_params' => array(
62+
'email' => LANG_RESTORE_BLOCK.($this->user['lock_reason'] ? '. '.$this->user['lock_reason'] : '')
63+
));
64+
65+
} elseif($this->user['pass_token']) {
66+
67+
return array('request_params' => array(
68+
'email' => LANG_RESTORE_TOKEN_IS_SEND
69+
));
70+
71+
}
72+
73+
return false;
74+
75+
}
76+
77+
public function run(){
78+
79+
$pass_token = string_random(32, $this->user['email']);
80+
81+
$this->users_model->updateUserPassToken($this->user['id'], $pass_token);
82+
83+
$messenger = cmsCore::getController('messages');
84+
85+
$to = array('email' => $this->user['email'], 'name' => $this->user['nickname']);
86+
$letter = array('name' => 'reg_restore');
87+
88+
$messenger->sendEmail($to, $letter, array(
89+
'nickname' => $this->user['nickname'],
90+
'page_url' => href_to_abs('auth', 'reset', $pass_token),
91+
'valid_until' => html_date(date('d.m.Y H:i', time() + (24 * 3600)), true),
92+
));
93+
94+
$this->result = array(
95+
'user_id' => $this->user['id'],
96+
'success' => true,
97+
'success_text' => LANG_TOKEN_SENDED,
98+
'sig' => get_sig()
99+
);
100+
101+
}
102+
103+
}

package/system/languages/en/controllers/api/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
define('LANG_API_ERROR1', 'Unknown error occurred');
2323
define('LANG_API_ERROR3', 'Unknown method passed ');
24+
define('LANG_API_ERROR5', 'User authorization failed');
2425
define('LANG_API_ERROR101', 'Invalid API KEY');
2526
define('LANG_API_ERROR2', 'Access key is disabled');
2627
define('LANG_API_ERROR23', 'This method was disabled ');

package/system/languages/ru/controllers/api/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
define('LANG_API_ERROR1', 'Произошла неизвестная ошибка');
2323
define('LANG_API_ERROR3', 'Передан неизвестный метод');
24+
define('LANG_API_ERROR5', 'Авторизация пользователя не удалась');
2425
define('LANG_API_ERROR101', 'Неверный ключ доступа');
2526
define('LANG_API_ERROR2', 'Ключ доступа выключен');
2627
define('LANG_API_ERROR23', 'Метод был выключен');

0 commit comments

Comments
 (0)