-
Notifications
You must be signed in to change notification settings - Fork 23
/
user.logic.php
70 lines (63 loc) · 1.85 KB
/
user.logic.php
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
<?php
require_once('predis/autoload.php');
require_once('util4p/CRObject.class.php');
require_once('util4p/ReSession.class.php');
require_once('util4p/CRLogger.class.php');
require_once('Code.class.php');
require_once('UserManager.class.php');
require_once('config.inc.php');
require_once('init.inc.php');
/* Get or Create User from Open_id */
function user_get(CRObject $info)
{
$res['user'] = UserManager::getByOpenID($info->get('open_id'));
if ($res['user'] === null) {
/* if user(UID=1) not exist, set this user as admin
* WARN: do not delete user(UID=1) */
if (UserManager::getByUID(1) === null) {
$info->set('role', 'admin');
}
if (!UserManager::add($info)) {
$res['errno'] = Code::FAIL;
return $res;
}
$res['user'] = UserManager::getByOpenID($info->get('open_id'));
}
$res['errno'] = $res['user'] !== null ? Code::SUCCESS : Code::UNKNOWN_ERROR;
return $res;
}
function user_signout()
{
Session::expire();
$res['errno'] = Code::SUCCESS;
return $res;
}
function log_gets(CRObject $rule)
{
if (Session::get('uid') === null) {
$res['errno'] = Code::NOT_LOGED;
return $res;
}
if ($rule->get('who') !== 'all') {
$rule->set('who', 'self');
$rule->set('scope', Session::get('uid'));
$rule->set('tag', 'user.login');
}
if ($rule->get('who') === 'all' && !AccessController::hasAccess(Session::get('role', 'visitor'), 'logs.get_others')) {
$res['errno'] = Code::NO_PRIVILEGE;
return $res;
}
$res['errno'] = Code::SUCCESS;
$res['count'] = CRLogger::getCount($rule);
$res['logs'] = CRLogger::search($rule);
return $res;
}
function oauth_get_url()
{
$state = Random::randomString(16);
Session::put('oauth:state', $state);
$url = OAUTH_SITE . '/login?response_type=code&client_id=' . OAUTH_CLIENT_ID . '&redirect_uri=' . BASE_URL . '/auth&state=' . $state . '&scope=role';
$res['errno'] = Code::SUCCESS;
$res['url'] = $url;
return $res;
}