00001 <?php
00002 Load::models('permanentlogins');
00003
00004
00005
00006
00007
00008
00009
00010 class Users {
00011 const STATUS_ACTIVE = 'ACTIVE';
00012 const STATUS_DELETED = 'DELETED';
00013 const STATUS_DISABLED = 'DISABLED';
00014 const STATUS_UNCONFIRMED = 'UNCONFIRMED';
00015
00016 const EMAIL_STATUS_UNCONFIRMED = 'UNCONFIRMED';
00017 const EMAIL_STATUS_CONFIRMED = 'CONFIRMED';
00018 const EMAIL_STATUS_EXPIRED = 'EXPIRED';
00019 const EMAIL_STATUS_BOUNCED = 'BOUNCED';
00020
00021
00022
00023
00024
00025
00026 public static function get_current_user() {
00027 return AccessControl::get_current_aro();
00028 }
00029
00030
00031
00032
00033
00034
00035 public static function get_current_user_id() {
00036 $ret = false;
00037 $user = self::get_current_user();
00038 if ($user) {
00039 $ret = $user->id;
00040 }
00041 return $ret;
00042 }
00043
00044
00045
00046
00047
00048
00049
00050 public static function is_logged_in() {
00051 $user = self::get_current_user();
00052 return ($user instanceof DAOUsers);
00053 }
00054
00055
00056
00057
00058 public static function is_current($user) {
00059 $ret = false;
00060 if ($user) {
00061 $current = self::get_current_user();
00062 if ($current) {
00063 $ret = ($current->id == $user->id);
00064 }
00065 }
00066 return $ret;
00067 }
00068
00069
00070
00071
00072 public static function current_has_role($role) {
00073 if (self::is_logged_in()) {
00074 return self::get_current_user()->has_role($role);
00075 }
00076 return false;
00077 }
00078
00079
00080
00081
00082 public static function reload_current() {
00083 if (self::is_logged_in()) {
00084 $user = self::get(self::get_current_user_id());
00085 if ($user) {
00086 self::do_login($user);
00087 }
00088 else {
00089 self::logout();
00090 }
00091 }
00092 }
00093
00094
00095
00096
00097
00098
00099 public static function get($id) {
00100 return DB::get_item('users', 'id', $id);
00101 }
00102
00103
00104
00105
00106
00107
00108 public static function get_statuses() {
00109 return array(
00110 self::STATUS_ACTIVE => tr(self::STATUS_ACTIVE, 'users'),
00111 self::STATUS_DELETED => tr(self::STATUS_DELETED, 'users'),
00112 self::STATUS_DISABLED => tr(self::STATUS_DISABLED, 'users'),
00113 self::STATUS_UNCONFIRMED => tr(self::STATUS_UNCONFIRMED, 'users'),
00114 );
00115 }
00116
00117
00118
00119
00120
00121
00122 public static function get_email_statuses() {
00123 return array(
00124 self::EMAIL_STATUS_UNCONFIRMED => tr(self::EMAIL_STATUS_UNCONFIRMED, 'users'),
00125 self::EMAIL_STATUS_CONFIRMED => tr(self::EMAIL_STATUS_CONFIRMED, 'users'),
00126 self::EMAIL_STATUS_EXPIRED => tr(self::EMAIL_STATUS_EXPIRED, 'users'),
00127 self::EMAIL_STATUS_BOUNCED => tr(self::EMAIL_STATUS_BOUNCED, 'users'),
00128 );
00129 }
00130
00131
00132
00133
00134 public static function logout() {
00135 $cmd = CommandsFactory::create_command('users', 'logout', false);
00136 $cmd->execute();
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 public static function login($params, $permanent) {
00148 $params['permanent'] = $permanent;
00149 $cmd = CommandsFactory::create_command('users', 'login', $params);
00150 $ret = $cmd->execute();
00151
00152 if ($ret->is_ok() && $permanent) {
00153 $user = self::get_current_user();
00154 PermanentLogins::enable_permanent_login($user);
00155 }
00156
00157 return $ret;
00158 }
00159
00160
00161
00162
00163
00164
00165 public static function do_login($user) {
00166 $cmd = CommandsFactory::create_command($user, 'loginknown', array());
00167 $ret = $cmd->execute();
00168 return $ret->is_ok();
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178 public static function create($params, &$result) {
00179 $cmd = CommandsFactory::create_command('users', 'create', $params);
00180 $err = $cmd->execute();
00181 $result = $cmd->get_result();
00182 return $err;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 public static function register($username, $password, $email, &$result) {
00195 $params = array(
00196 'name' => $username,
00197 'email' => $email,
00198 'password' => $password
00199 );
00200 $cmd = CommandsFactory::create_command('users', 'register', $params);
00201 $err = $cmd->execute();
00202 $result = $cmd->get_result();
00203 return $err;
00204 }
00205
00206
00207
00208
00209
00210
00211 public static function update(DAOUsers $user, $params) {
00212 $cmd = CommandsFactory::create_command($user, 'update', $params);
00213 $err = $cmd->execute();
00214
00215
00216 if ($err->is_ok()) {
00217 if (self::is_current($user)) {
00218 self::do_login(clone($user));
00219 }
00220 }
00221
00222 return $err;
00223 }
00224
00225
00226
00227
00228
00229
00230 public static function create_adapter() {
00231 $users = new DAOUsers();
00232 $users->status = self::STATUS_ACTIVE;
00233 return $users;
00234 }
00235
00236
00237
00238
00239
00240
00241 public static function create_all_user_adapter() {
00242 $users = new DAOUsers();
00243 return $users;
00244 }
00245
00246
00247
00248
00249
00250
00251 public static function create_role_adapter($roles) {
00252 $users = new DAOUsers();
00253
00254 $all_roles = self::get_user_roles();
00255 $arr_roles_in = array();
00256 foreach(Arr::force($roles, false) as $r) {
00257 $key = array_search($r, $all_roles);
00258 if ($key) {
00259 $arr_roles_in[] = $key;
00260 }
00261 }
00262 if (count($arr_roles_in) > 0) {
00263 $link = new DAOUsers2userroles();
00264 $link->add_where('id_role', DBWhere::OP_IN, $arr_roles_in);
00265 $users->status = self::STATUS_ACTIVE;
00266 $users->join($link);
00267 }
00268 else {
00269 $users->add_where('1 = 2');
00270 }
00271
00272 return $users;
00273 }
00274
00275
00276
00277
00278
00279
00280 public static function count_unconfirmed() {
00281 $users = new DAOUsers();
00282 $users->status = USER_STATUS_UNCONFIRMED;
00283 return $users->count();
00284 }
00285
00286
00287
00288
00289
00290
00291 public static function get_user_roles() {
00292 $ret = array();
00293 $dao = new DAOUserroles();
00294 $dao->find();
00295 while($dao->fetch()) {
00296 $ret[$dao->id] = tr($dao->name, 'app');
00297 }
00298 return $ret;
00299 }
00300
00301
00302
00303
00304 public static function initialize() {
00305 $current_user_id = Session::peek('current_user_id');
00306 if (empty($current_user_id)) {
00307
00308 $current_user = Session::pull('current_user');
00309 }
00310 else {
00311 $current_user = self::get($current_user_id);
00312 }
00313
00314 if (empty($current_user)) {
00315 self::check_permanent_login();
00316 }
00317 else {
00318 self::do_login($current_user);
00319 }
00320 }
00321
00322
00323
00324
00325 private static function check_permanent_login() {
00326 $permanent = PermanentLogins::get_current();
00327 if ($permanent) {
00328 if ($user = self::get($permanent->id_user)) {
00329 $cmd = CommandsFactory::create_command($user, 'restartsession', false);
00330 $cmd->execute();
00331 self::do_login($user);
00332 }
00333 }
00334 }
00335
00336
00337
00338
00339 public static function login_as_system() {
00340 Load::models('systemusers');
00341 $user = new DAOSystemUsers();
00342 self::do_login($user);
00343 }
00344
00345
00346
00347
00348
00349
00350
00351 public static function lost_password($email) {
00352 $ret = new Status();
00353
00354 $user = new DAOUsers();
00355 $user->email = $email;
00356 $user->status = self::STATUS_ACTIVE;
00357 if ($email && $user->find(IDataObject::AUTOFETCH)) {
00358 $params = array(
00359 'id_item' => $user->id,
00360 'action' => 'onetimelogin',
00361 'data' => $email
00362 );
00363 $cmd = CommandsFactory::create_command('confirmations', 'create', $params);
00364 $ret->merge($cmd->execute());
00365 }
00366 else {
00367 $ret->append(tr('Unknown email', 'users'));
00368 }
00369 return $ret;
00370 }
00371
00372
00373
00374
00375
00376
00377
00378 public static function resend_registration_mail($email) {
00379 $user = new DAOUsers();
00380 $user->email = $email;
00381 $ret = new Status();
00382 if ($email && $user->find(IDataObject::AUTOFETCH)) {
00383 switch ($user->status) {
00384 case Users::STATUS_ACTIVE:
00385 $ret->append(tr('Your account already has been activated, use you user name and password to log in.', 'users'));
00386 $ret->persist();
00387 Url::create(ActionMapper::get_url('login'))->redirect();
00388 exit;
00389 break;
00390 case Users::STATUS_UNCONFIRMED:
00391 Load::models('confirmations');
00392 $confirmation = new DAOConfirmations();
00393 $confirmation->id_item = $user->id;
00394 $confirmation->action = 'createaccount';
00395 if ($confirmation->find(IDataObject::AUTOFETCH)) {
00396 $handler = $confirmation->create_handler();
00397 $ret->merge($handler->created());
00398 }
00399 else {
00400 $ret->append(tr('You activation request already has expired.', 'users'));
00401 }
00402 break;
00403 default:
00404
00405 $ret->append(tr('Unknown email', 'users'));
00406 break;
00407 }
00408 }
00409 else {
00410 $ret->append(tr('Unknown email', 'users'));
00411 }
00412 return $ret;
00413 }
00414
00415
00416
00417
00418
00419
00420
00421
00422 public static function create_hash_algorithm($hash_type) {
00423
00424 $hash_type = strtolower($hash_type);
00425 Load::classes_in_directory('behaviour/commands/users/hashes', $hash_type, 'hash', true);
00426 $cls_name = Load::filename_to_classname($hash_type, 'hash');
00427
00428 return new $cls_name();
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438 public static function create_hash($source, $hash_type) {
00439 $algo = self::create_hash_algorithm($hash_type);
00440 return $algo->hash($source);
00441 }
00442
00443
00444
00445
00446
00447
00448
00449 public static function confirm_email($user) {
00450 $cmd = CommandsFactory::create_command($user, 'confirmemail', false);
00451 return $cmd->execute();
00452 }
00453
00454
00455
00456
00457
00458
00459 public static function is_unique_username($name) {
00460 $user = new DAOUsers();
00461 $user->add_where('status', '!=', Users::STATUS_DELETED);
00462 $user->name = $name;
00463 return ($user->count() == 0);
00464 }
00465 }