Beispiel #1
0
 /**
  * Returns a user by their UID.
  *
  * ```php
  * $user = craft()->users->getUserByUid($userUid);
  * ```
  *
  * @param int $uid The user’s UID.
  *
  * @return UserModel|null The user with the given UID, or `null` if a user could not be found.
  */
 public function getUserByUid($uid)
 {
     $userRecord = UserRecord::model()->findByAttributes(array('uid' => $uid));
     if ($userRecord) {
         return UserModel::populateModel($userRecord);
     }
     return null;
 }
 /**
  * Handle the onBeforeSaveUser event.
  *
  * @param Event $event
  */
 public function onBeforeSaveUser(Event $event)
 {
     // Get user id to save
     $id = $event->params['user']->id;
     if (!$event->params['isNewUser']) {
         // Get old user from db
         $user = UserModel::populateModel(UserRecord::model()->findById($id));
         // Get fields
         $this->before = $this->fields($user);
     } else {
         // Get fields
         $this->before = $this->fields($event->params['user'], true);
     }
 }
 /**
  * Gets the currently logged-in user.
  *
  * @return UserModel|null
  */
 public function getUser()
 {
     // Does a user appear to be logged in?
     if (Craft::isInstalled() && $this->getState('__id') !== null) {
         if (!isset($this->_user)) {
             $userRow = $this->_getUserRow($this->getId());
             if ($userRow) {
                 $this->_userModel = UserModel::populateModel($userRow);
             } else {
                 $this->_userModel = false;
             }
         }
         return $this->_userModel ? $this->_userModel : null;
     }
 }
 /**
  * Returns the currently logged-in user.
  *
  * @return UserModel|null The currently logged-in user, or `null`.
  */
 public function getUser()
 {
     // Does a user appear to be logged in?
     if (craft()->isInstalled() && !$this->getIsGuest()) {
         if (!isset($this->_userModel)) {
             $userRow = $this->_getValidUserRow($this->getId());
             // Only return active and pending users.
             if ($userRow) {
                 $this->_userModel = UserModel::populateModel($userRow);
             } else {
                 $this->_userModel = false;
             }
         }
         return $this->_userModel ? $this->_userModel : null;
     }
 }
 /**
  * @inheritDoc IElementType::populateElementModel()
  *
  * @param array $row
  *
  * @return array
  */
 public function populateElementModel($row)
 {
     return UserModel::populateModel($row);
 }
Beispiel #6
0
 /**
  * Returns the currently logged-in user.
  *
  * @return UserModel|null The currently logged-in user, or `null`.
  */
 public function getUser()
 {
     // Does a user appear to be logged in?
     if (craft()->isInstalled() && !$this->getIsGuest()) {
         if (!isset($this->_userModel)) {
             $userRow = $this->_getUserRow($this->getId());
             if ($userRow && $userRow['status'] == UserStatus::Active || $userRow['status'] == UserStatus::Pending) {
                 $this->_userModel = UserModel::populateModel($userRow);
             } else {
                 $this->_userModel = false;
             }
         }
         return $this->_userModel ? $this->_userModel : null;
     }
 }
 /**
  * Returns the currently logged-in user.
  *
  * @return UserModel|null The currently logged-in user, or `null`.
  */
 public function getUser()
 {
     // Does a user appear to be logged in?
     if (craft()->isInstalled() && !$this->getIsGuest()) {
         if (!isset($this->_userModel)) {
             $userRow = $this->_getUserRow($this->getId());
             // Only return active and pending users.
             if ($userRow) {
                 $validUser = false;
                 // Keeping extra logic here so the upgrade to 2.3 won't freak.
                 // First the pre 2.3 check.
                 if (isset($userRow['status']) && $userRow['status'] == UserStatus::Active || isset($userRow['status']) && $userRow['status'] == UserStatus::Pending) {
                     $validUser = true;
                 }
                 // Now the 2.3 check. If all 3 of these are false, then the user is active or pending.
                 if (isset($userRow['suspended']) && isset($userRow['archived']) && isset($userRow['locked']) && (!$userRow['suspended'] && !$userRow['archived'] && !$userRow['locked'])) {
                     $validUser = true;
                 }
                 if ($validUser) {
                     $this->_userModel = UserModel::populateModel($userRow);
                 }
             } else {
                 $this->_userModel = false;
             }
         }
         return $this->_userModel ? $this->_userModel : null;
     }
 }
 /**
  * Returns the currently logged-in user.
  *
  * @return UserModel|null The currently logged-in user, or `null`.
  */
 public function getUser()
 {
     // Does a user appear to be logged in?
     if (craft()->isInstalled() && !$this->getIsGuest()) {
         if (!isset($this->_userModel)) {
             $userRow = $this->_getUserRow($this->getId());
             // Only return active and pending users.
             if ($userRow) {
                 $validUser = false;
                 // TODO: Remove after next breakpoint.
                 // Keeping extra logic here so the upgrade to 2.3 won't freak.
                 // First the pre 2.3 check.
                 if (isset($userRow['status']) && $userRow['status'] == UserStatus::Active || isset($userRow['status']) && $userRow['status'] == UserStatus::Pending) {
                     $validUser = true;
                 }
                 // Now the 2.3 check. If all 3 of these are false, then the user is active or pending.
                 if (isset($userRow['suspended']) && isset($userRow['archived']) && isset($userRow['locked']) && (!$userRow['suspended'] && !$userRow['archived'] && !$userRow['locked'])) {
                     $validUser = true;
                 }
                 // One last attempt.
                 if (!$validUser) {
                     // If the previous user was an admin and we're impersonating the current user.
                     if ($previousUserId = craft()->httpSession->get(static::USER_IMPERSONATE_KEY)) {
                         $previousUser = craft()->users->getUserById($previousUserId);
                         if ($previousUser && $previousUser->admin) {
                             $validUser = true;
                         }
                     }
                 }
                 if ($validUser) {
                     $this->_userModel = UserModel::populateModel($userRow);
                 }
             } else {
                 $this->_userModel = false;
             }
         }
         return $this->_userModel ? $this->_userModel : null;
     }
 }
 /**
  * Finds a user.
  *
  * @param UserCriteria|null $criteria
  * @return array
  */
 public function findUser(UserCriteria $criteria = null)
 {
     if (!$criteria) {
         $criteria = new UserCriteria();
     }
     $query = craft()->db->createCommand()->select('u.*')->from('users u');
     $this->_applyUserConditions($query, $criteria);
     $result = $query->queryRow();
     if ($result) {
         return UserModel::populateModel($result);
     }
 }
 /**
  * Initialize the category saving/deleting events.
  */
 public function log()
 {
     // Get values before saving
     craft()->on('users.onBeforeSaveUser', function (Event $event) {
         // Get user id to save
         $id = $event->params['user']->id;
         if (!$event->params['isNewUser']) {
             // Get old user from db
             $user = UserModel::populateModel(UserRecord::model()->findById($id));
             // Get fields
             craft()->auditLog_user->before = craft()->auditLog_user->fields($user);
         } else {
             // Get fields
             craft()->auditLog_user->before = craft()->auditLog_user->fields($event->params['user'], true);
         }
     });
     // Get values after saving
     craft()->on('users.onSaveUser', function (Event $event) {
         // Get saved user
         $user = $event->params['user'];
         // Get fields
         craft()->auditLog_user->after = craft()->auditLog_user->fields($user);
         // New row
         $log = new AuditLogRecord();
         // Set user id
         $log->userId = craft()->userSession->getUser() ? craft()->userSession->getUser()->id : $user->id;
         // Set element type
         $log->type = ElementType::User;
         // Set origin
         $log->origin = craft()->request->isCpRequest() ? craft()->config->get('cpTrigger') . '/' . craft()->request->path : craft()->request->path;
         // Set before
         $log->before = craft()->auditLog_user->before;
         // Set after
         $log->after = craft()->auditLog_user->after;
         // Set status
         $log->status = $event->params['isNewUser'] ? AuditLogModel::CREATED : AuditLogModel::MODIFIED;
         // Save row
         $log->save(false);
         // Callback
         craft()->auditLog->elementHasChanged(ElementType::User, $user->id, craft()->auditLog_user->before, craft()->auditLog_user->after);
     });
     // Get values before deleting
     craft()->on('users.onBeforeDeleteUser', function (Event $event) {
         // Get deleted user
         $user = $event->params['user'];
         // Get fields
         craft()->auditLog_user->before = craft()->auditLog_user->fields($user);
         craft()->auditLog_user->after = craft()->auditLog_user->fields($user, true);
         // New row
         $log = new AuditLogRecord();
         // Set user id
         $log->userId = craft()->userSession->getUser()->id;
         // Set element type
         $log->type = ElementType::User;
         // Set origin
         $log->origin = craft()->request->isCpRequest() ? craft()->config->get('cpTrigger') . '/' . craft()->request->path : craft()->request->path;
         // Set before
         $log->before = craft()->auditLog_user->before;
         // Set after
         $log->after = craft()->auditLog_user->after;
         // Set status
         $log->status = AuditLogModel::DELETED;
         // Save row
         $log->save(false);
         // Callback
         craft()->auditLog->elementHasChanged(ElementType::User, $user->id, craft()->auditLog_user->before, craft()->auditLog_user->after);
     });
 }