Answers.Body: <p>Searching the class reference documentation, I see that the user is available from the <a href="http://buildwithcraft.com/classreference/services/UserSessionService" rel="nofollow">userSession service</a> — but never tried this personally.</p>
  
  <pre><code>craft()-&gt;on('users.beforeActivateUser', function(Event $event){
      // stop the user from being able to activate their account. Forces admins to do so.
      if(!craft()-&gt;userSession-&gt;isAdmin()) {
          $event-&gt;performAction = false;
      }
  });
  </code></pre>
  
  <p>or to just get the current user for other testing, <code>craft()-&gt;userSession-&gt;user</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-13T18:21:21.960Z
Answers.LastActivityDate: 2015-02-13T18:29:08.643Z
Answers.LastEditDate: 2015-02-13T18:29:08.643Z
Answers.UserDisplayName: Douglas McDonald
Answers.Body: <p>Just wanted to add to this thread...</p>
  
  <p>Depending on which event you are listening to/watching for in your plugin, you might have to use a different approach to getting the user. For example, the <code>onActivateUser</code> event won't have a user's session to check just yet. But, you <em>do</em> have access to the event params...</p>
  
  <p><strong>MyPlugin.php</strong></p>
  
  <pre><code>public function init() {
      ...
  
      craft()-&gt;on('users.onActivateUser', function (Event $event) {
          if (craft()-&gt;config-&gt;get('devMode')) {
          error_log('----- users.onActivateUser called -----');
      }
  
      // Get the user.
      $user = craft()-&gt;users-&gt;getUserById($event-&gt;params['user']-&gt;id);
  
      // Check for user group.
      if ($user &amp;&amp; $user-&gt;isInGroup('groupHandle')) {
          // Do stuff..
      } else {
          // User does not belong to this group.
      }
  
      ...
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-23T13:32:05.880Z
Answers.LastActivityDate: 2016-08-23T18:21:11.130Z
Answers.LastEditDate: 2016-08-23T18:21:11.130Z
Answers.UserDisplayName: Damon
Answers.Body: <p>I'm a bit confused by your question.</p>
  
  <p>It sounds as though you want to check if the currently logged-in user is an Administrator, but it looks like you're actually checking whether the <em>newly-registered</em> user is an Administrator.</p>
  
  <p>Here's how to <a href="http://buildwithcraft.com/classreference/services/UserSessionService#getUser-detail">retrieve the currently logged-in User</a>, and <a href="http://buildwithcraft.com/classreference/models/UserModel#isInGroup-detail">check whether they belong</a> to a specific User Group.</p>
  
  <pre><code>function (Event $event)
  {
      $user = craft()-&gt;userSession-&gt;getUser();
  
      if ($user &amp;&amp; $user-&gt;isInGroup('groupHandle') {
          // Do stuff.
      }
  }
  </code></pre>
  
  <p>Alternatively, you could <a href="http://buildwithcraft.com/classreference/services/UserPermissionsService#doesGroupHavePermission-detail">check whether the current user has permission</a> to perform a specific action:</p>
  
  <pre><code>if ($user &amp;&amp; craft()-&gt;userPermissions-&gt;doesUserHavePermission($user-&gt;id, 'registerUsers')) {
      // Do stuff.
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-13T18:45:05.943Z
Answers.LastActivityDate: 2015-02-13T18:45:05.943Z
Answers.UserDisplayName: Stephen Lewis
Body: <p>I have a public registration form and a plugin which needs to create an entry for a user when that user is activated. That part works. Because I want to force an Admin to activate the user, I am using this code block to stop Craft automatically activating a user on registration:</p>
  
  <pre><code>public function init() {
      craft()-&gt;on('users.beforeActivateUser', function(Event $event){
          // stop the user from being able to activate their account. Forces admins to do so.
          $event-&gt;performAction = false;
      });
  }
  </code></pre>
  
  <p>But this also stops an Admin from activating the user account via the control panel.</p>
  
  <p>I can't work out how to test that the user performing the event is the admin and therefore skip that blocking code. I'd tried this:</p>
  
  <pre><code>public function init() {
      craft()-&gt;on('users.beforeActivateUser', function(Event $event){
          // stop the user from being able to activate their account. Forces admins to do so.
          if($event-&gt;params['user']-&gt;isInGroup('artists')){
              $event-&gt;performAction = false;
          }
      });
  }
  </code></pre>
  
  <p>But that doesn't work, presumably because a new registering user doesn't belong to any group at the point that code is run.</p>
  
Tags: <plugin-development><users>
Title: How to identify the logged in user inside a plugin?
ThreadText: # How to identify the logged in user inside a plugin?
  
  (Asked by: username_0 on 2015-02-13)
  
  I have a public registration form and a plugin which needs to create an entry for a user when that user is activated. That part works. Because I want to force an Admin to activate the user, I am using this code block to stop Craft automatically activating a user on registration:
  
  ```
  public function init() {
      craft()->on('users.beforeActivateUser', function(Event $event){
          // stop the user from being able to activate their account. Forces admins to do so.
          $event->performAction = false;
      });
  }
  
  ```
  
  But this also stops an Admin from activating the user account via the control panel.
  
  I can't work out how to test that the user performing the event is the admin and therefore skip that blocking code. I'd tried this:
  
  ```
  public function init() {
      craft()->on('users.beforeActivateUser', function(Event $event){
          // stop the user from being able to activate their account. Forces admins to do so.
          if($event->params['user']->isInGroup('artists')){
              $event->performAction = false;
          }
      });
  }
  
  ```
  
  But that doesn't work, presumably because a new registering user doesn't belong to any group at the point that code is run.
  
  **Tags:** plugin-development, users
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2015-02-13. Score: 13 | Accepted answer
  
  I'm a bit confused by your question.
  
  It sounds as though you want to check if the currently logged-in user is an Administrator, but it looks like you're actually checking whether the *newly-registered* user is an Administrator.
  
  Here's how to [retrieve the currently logged-in User](http://buildwithcraft.com/classreference/services/UserSessionService#getUser-detail), and [check whether they belong](http://buildwithcraft.com/classreference/models/UserModel#isInGroup-detail) to a specific User Group.
  
  ```
  function (Event $event)
  {
      $user = craft()->userSession->getUser();
  
      if ($user && $user->isInGroup('groupHandle') {
          // Do stuff.
      }
  }
  
  ```
  
  Alternatively, you could [check whether the current user has permission](http://buildwithcraft.com/classreference/services/UserPermissionsService#doesGroupHavePermission-detail) to perform a specific action:
  
  ```
  if ($user && craft()->userPermissions->doesUserHavePermission($user->id, 'registerUsers')) {
      // Do stuff.
  }
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2015-02-13. Score: 2
  
  Searching the class reference documentation, I see that the user is available from the [userSession service](http://buildwithcraft.com/classreference/services/UserSessionService) — but never tried this personally.
  
  ```
  craft()->on('users.beforeActivateUser', function(Event $event){
      // stop the user from being able to activate their account. Forces admins to do so.
      if(!craft()->userSession->isAdmin()) {
          $event->performAction = false;
      }
  });
  
  ```
  
  or to just get the current user for other testing, `craft()->userSession->user`.
  
  
  ---
  
  ### Answer by username_2 on 2016-08-23. Score: 1
  
  Just wanted to add to this thread...
  
  Depending on which event you are listening to/watching for in your plugin, you might have to use a different approach to getting the user. For example, the `onActivateUser` event won't have a user's session to check just yet. But, you *do* have access to the event params...
  
  **MyPlugin.php**
  
  ```
  public function init() {
      ...
  
      craft()->on('users.onActivateUser', function (Event $event) {
          if (craft()->config->get('devMode')) {
          error_log('----- users.onActivateUser called -----');
      }
  
      // Get the user.
      $user = craft()->users->getUserById($event->params['user']->id);
  
      // Check for user group.
      if ($user && $user->isInGroup('groupHandle')) {
          // Do stuff..
      } else {
          // User does not belong to this group.
      }
  
      ...
  }
  
  ```
  
  
  ---
  
  