예제 #1
0
 /**
  * Return current user if it exists.
  * 
  * @return User instance or false
  */
 public static function user()
 {
     if (is_null(self::$user)) {
         // Not already cached
         self::$user = false;
         // Authentication logic
         $event = new Event('auth_check');
         $auth = $event->trigger(function () {
             // No authentification is required by application
             if (!Config::get('auth_sp_type')) {
                 return array();
             }
             // Check for local authentificaiton (script)
             if (AuthLocal::isAuthenticated()) {
                 return array('local', AuthLocal::attributes());
             }
             // Check for remote application/user
             if ((Config::get('auth_remote_application_enabled') || Config::get('auth_remote_user_enabled')) && AuthRemote::isAuthenticated() && (AuthRemote::application() && Config::get('auth_remote_application_enabled') || !AuthRemote::application() && Config::get('auth_remote_user_enabled'))) {
                 return array('remote', AuthRemote::attributes(), AuthRemote::application() && AuthRemote::isAdmin());
             }
             // Check for SP autentification
             if (AuthSP::isAuthenticated()) {
                 return array('sp', AuthSP::attributes());
             }
             return array();
         });
         self::$type = array_shift($auth);
         self::$attributes = array_shift($auth);
         if (count($auth)) {
             self::$isAdmin = array_shift($auth);
         }
         if (self::$attributes && array_key_exists('uid', self::$attributes)) {
             $user_filter = Config::get('auth_user_filter');
             if ($user_filter) {
                 self::$allowed = false;
                 if (is_string($user_filter)) {
                     if (preg_match('`^([^:]+):(.+)$`', $user_filter, $p)) {
                         self::$allowed = array_key_exists($p[1], self::$attributes) && preg_match('`' . $p[2] . '`', self::$attributes[$p[1]]);
                     }
                 } else {
                     self::$allowed = !(bool) $user_filter;
                 }
                 if (!self::$allowed) {
                     self::$type = null;
                     return;
                 }
             }
             // Set user if got uid attribute
             self::$user = User::fromAttributes(self::$attributes);
             // Save user additionnal attributes if enabled
             if (self::isSP() && Config::get('auth_sp_save_user_additional_attributes') && array_key_exists('additional', self::$attributes) && self::$user->additional_attributes != self::$attributes['additional']) {
                 self::$user->additional_attributes = self::$attributes['additional'];
                 self::$user->save();
             }
         }
     }
     return self::$user;
 }
예제 #2
0
 /**
  * Set local user
  * 
  * @param string $user_id user id
  * @param string $email user email
  * @param string $name user name
  * 
  */
 public static function setUser($user_id, $email, $name = null)
 {
     if (is_null($user_id)) {
         // Virtually closes the local session
         self::$attributes = null;
     } else {
         self::$attributes = array('uid' => $user_id, 'email' => $email, 'name' => $name);
     }
 }