示例#1
0
 /**
  * Authenticates the user with the provided Authenticator class.
  *
  * @param int $UserID The UserID to start the session with.
  * @param bool $SetIdentity Whether or not to set the identity (cookie) or make this a one request session.
  * @param bool $Persist If setting an identity, should we persist it beyond browser restart?
  */
 public function start($UserID = false, $SetIdentity = true, $Persist = false)
 {
     if (!c('Garden.Installed', false)) {
         return;
     }
     // Retrieve the authenticated UserID from the Authenticator module.
     $UserModel = Gdn::authenticator()->getUserModel();
     $this->UserID = $UserID !== false ? $UserID : Gdn::authenticator()->getIdentity();
     $this->User = false;
     // Now retrieve user information
     if ($this->UserID > 0) {
         // Instantiate a UserModel to get session info
         $this->User = $UserModel->getSession($this->UserID);
         if ($this->User) {
             if ($SetIdentity) {
                 Gdn::authenticator()->setIdentity($this->UserID, $Persist);
                 Logger::event('session_start', Logger::INFO, 'Session started for {username}.');
                 Gdn::pluginManager()->callEventHandlers($this, 'Gdn_Session', 'Start');
             }
             $UserModel->EventArguments['User'] =& $this->User;
             $UserModel->fireEvent('AfterGetSession');
             $this->permissions->setPermissions($this->User->Permissions);
             $this->_Preferences = $this->User->Preferences;
             $this->_Attributes = $this->User->Attributes;
             $this->_TransientKey = is_array($this->_Attributes) ? val('TransientKey', $this->_Attributes) : false;
             if ($this->_TransientKey === false) {
                 $this->_TransientKey = $UserModel->setTransientKey($this->UserID);
             }
             // Save any visit-level information.
             if ($SetIdentity) {
                 $UserModel->updateVisit($this->UserID);
             }
         } else {
             $this->UserID = 0;
             $this->User = false;
             $this->_TransientKey = getAppCookie('tk');
             if ($SetIdentity) {
                 Gdn::authenticator()->setIdentity(null);
             }
         }
     } else {
         // Grab the transient key from the cookie. This doesn't always get set but we'll try it here anyway.
         $this->_TransientKey = getAppCookie('tk');
     }
     // Load guest permissions if necessary
     if ($this->UserID == 0) {
         $guestPermissions = $UserModel->getPermissions(0);
         $this->permissions->setPermissions($guestPermissions->getPermissions());
     }
 }
示例#2
0
 /**
  * Get a user's permissions.
  *
  * @param int $userID Unique ID of the user.
  * @return Vanilla\Permissions
  */
 public function getPermissions($userID)
 {
     $permissions = new Vanilla\Permissions();
     $permissionsKey = '';
     if (Gdn::cache()->activeEnabled()) {
         $permissionsIncrement = $this->getPermissionsIncrement();
         $permissionsKey = formatString(self::USERPERMISSIONS_KEY, ['UserID' => $userID, 'PermissionsIncrement' => $permissionsIncrement]);
         $cachedPermissions = Gdn::cache()->get($permissionsKey);
         if ($cachedPermissions !== Gdn_Cache::CACHEOP_FAILURE) {
             $permissions->setPermissions($cachedPermissions);
             return $permissions;
         }
     }
     $data = Gdn::permissionModel()->cachePermissions($userID);
     $permissions->compileAndLoad($data);
     $this->EventArguments['UserID'] = $userID;
     $this->EventArguments['Permissions'] = $permissions;
     $this->fireEvent('loadPermissions');
     if (Gdn::cache()->activeEnabled()) {
         Gdn::cache()->store($permissionsKey, $permissions->getPermissions());
     } else {
         // Save the permissions to the user table
         if ($userID > 0) {
             $this->SQL->put('User', ['Permissions' => dbencode($permissions->getPermissions())], ['UserID' => $userID]);
         }
     }
     return $permissions;
 }