-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.inc.php
164 lines (132 loc) · 4.44 KB
/
users.inc.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
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
<?php
/*
* users.inc.php
*
*/
require "User.php";
function checkLoggedIn() {
$user = $GLOBALS['usersCol']->findOne(array('_id' => $_SESSION['User']['_id']));
if(isset($_SESSION['User']) && ($user['Status'] == 1)) return true;
else return false;
}
function checkAdmin() {
$user = $GLOBALS['usersCol']->findOne(array('_id' => $_SESSION['User']['_id']));
if(isset($_SESSION['User']) && ($user['Status'] == 1) && (allowedRoles($user['Type'], $GLOBALS['ADMIN']))) return true;
else return false;
}
function createUser(&$f) {
$objUser = new User($f, True);
$aux = (array)$objUser;
$_SESSION['userId'] = $aux['id']; //OBSOLETE
$_SESSION['User'] = $aux;
unset($_SESSION['crypPassword']);
$dataDirId = prepUserWorkSpace($aux['id']);
if ($dataDirId){
$aux['dataDir']= $dataDirId;
saveNewUser($aux);
$_SESSION['User']['dataDir'] = $dataDirId;
sendWelcomeToNewUser($aux['_id'], $aux['Name'], $aux['Surname']);
}else{ echo 'error creating data dir!'; }
}
function createUserFromAdmin(&$f) {
$objUser = new User($f, True);
$aux = (array)$objUser;
$_SESSION['userId'] = $aux['id'];
$_SESSION['User'] = $aux;
unset($_SESSION['crypPassword']);
$dataDirId = prepUserWorkSpace($aux['id']);
if ($dataDirId){
$aux['dataDir']= $dataDirId;
saveNewUser($aux);
$_SESSION['User']['dataDir'] = $dataDirId;
sendPasswordToNewUser($aux['_id'], $aux['Name'], $aux['Surname'], $f['pass1']);
}else{ echo 'error creating data dir!'; }
}
function updateUser($f,$all) {
$aux = (array)$f;
unset($aux['crypPassword']);
unset($aux['lastLogin']);
$_SESSION['User'] = $aux;
$_SESSION['curDir'] = $_SESSION['User']['id'];
$GLOBALS['usersCol']->update(array('_id' => $f['_id']), $f, array('upsert=>1'));
if(!isset($_SESSION['lastUserLogin'])) $_SESSION['lastUserLogin'] = $all;
}
function logoutUser() {
session_unset();
// unset($_SESSION['User']);
// unset($_SESSION['lastUserLogin']);
}
function saveNewUser($userObj) {
return $GLOBALS['usersCol']->insert($userObj);
}
function checkUserLoginExists($login) {
//die("check user login exists");
if ($login)
$user = $GLOBALS['usersCol']->findOne(array('_id' => $login));
return ($user);
}
function loadUser($login, $pass) {
//$crpass = crypt($pass, PASSWORD_SALT);
$user = $GLOBALS['usersCol']->findOne(array('_id' => $login));
if (!$user['_id'] || !password_verify($pass, $user['crypPassword']) || ($user['Status'] == 0)) {
return False;
}else{
$auxlastlog = $user['lastLogin'];
$user['lastLogin'] = moment();
updateUser($user, $auxlastlog);
}
return $user;
}
function allowedRoles($role, $allowed){
if(in_array($role,$allowed)){
return true;
}else{
return false;
}
}
function getUser_diskQuota($login) {
$r = $GLOBALS['usersCol']->findOne(array('_id' => $login,
'diskQuota'=> array('$exists' => true)
));
if (isset($r['diskQuota']))
return $r['diskQuota'];
else
return false;
}
function saveUserJobs($login,$jobInfo) {
$GLOBALS['usersCol']->update(array('_id' => $login),
array('$set' => array('lastjobs' => $jobInfo)),
array('upsert' => 1));
}
function delUserJob($login,$pid) {
$GLOBALS['usersCol']->update(array('_id' => $login),
array('$unset' => array("lastjobs.$pid" => 1 ))
);
//array('$pull' => array("lastjobs" => $pid ))
//multi
}
function addUserJob($login,$data,$pid) {
$GLOBALS['usersCol']->update(array('_id' => $login),
array('$set' => array("lastjobs.$pid" => $data )),
array('upsert' => 1)
);
}
function getUserJobs($login) {
$r = $GLOBALS['usersCol']->findOne(array('_id' => $login,
'lastjobs'=> array('$exists' => true)
));
if (isset($r['lastjobs']))
return $r['lastjobs'];
else
return Array();
}
function getUserJobPid($login,$pid) {
$r = $GLOBALS['usersCol']->findOne(array("_id" => $login,
"lastjobs._id"=> $pid
));
if (isset($r['lastjobs']))
return $r['lastjobs'];
else
return Array();
}
?>