<p>Oh well, totally forgot to allow anonymous access at the top of the controller.
I guess I can leave this answer here in case somebody is as forgetful as me:</p>

<pre><code>protected $allowAnonymous = true;
</code></pre>

CC BY-SA 3.0
2016-11-23T21:46:50.457Z
Hah... funny thing is I've done that plenty of times.  Glad you're sorted!
Brad Bell
CC BY-SA 3.0
2016-11-23T21:52:12.000Z
yeah, one of those things that can drive you insane, especially if one, like me, is still learning.
Lukigi
CC BY-SA 3.0
2016-11-23T21:45:50.423Z
2016-11-23T21:47:05.450Z
2016-11-23T21:47:05.450Z
Lukigi
<p><a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/UsersService.php#L240" rel="nofollow noreferrer"><code>UsersService-&gt;saveUser()</code></a> (at <code>craft()-&gt;users-&gt;saveUser()</code>) doesn't do any sort of permissions checking or authentication checks because it assumes that all of that has been performed at the controller level.</p>

<p>You'll find this similar interactions between controllers and services elsewhere in Craft as well.</p>

<p>My guess is you're hitting some sort of validation error when trying to save a user with an authenticated session.  Try checking the output of <code>$user-&gt;getErrors()</code>.</p>

<p>You can, of course, in your plugin write your own service method that saves a user in a method that differs from Craft's implementation.</p>

CC BY-SA 3.0
2016-11-23T21:24:46.387Z
I appreciate your answer, but when I try to register a user while logged out I'm being redirected to a login form. I've updated the question with the actual code. This is the controller to which I direct the registration form.
Lukigi
CC BY-SA 3.0
2016-11-23T21:31:43.050Z
Maybe the example code isn't complete?  Where is `$success` being defined?
Brad Bell
CC BY-SA 3.0
2016-11-23T19:52:55.097Z
2016-11-23T19:52:55.097Z
Brad Bell
<p>In my controller, I can add new users when a user with the right privileges is logged in via <code>craft()-&gt;users-&gt;saveUser()</code>, but this doesn't work when no user is logged in.</p>

<p>Is "users/saveUser" the only service method I can use? I would prefer to be able to use my custom one.</p>

<p>EDIT:
code below works fine when I'm logged in as an admin user, otherwise it redirects me to login form, after form submission. Have I missed something in here?</p>

<pre><code>public function actionAddPartner() 
{
        $content = craft()-&gt;request-&gt;getPost();
        $companyId = explode("@", $content['email']);
        $user = new UserModel();
        $user-&gt;username         = $content['email'];
        $user-&gt;email            = $content['email'];
        $user-&gt;unverifiedEmail  = $content['email'];
        $user-&gt;firstName        = $content['firstName'];
        $user-&gt;lastName         = $content['lastName'];
        $user-&gt;pending          = true;
        $success = craft()-&gt;users-&gt;saveUser($user);
        if ($success) {
            // Assign them to the Partner Admin user group
            craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, 1);
            // Update the Element Index
            craft()-&gt;search-&gt;indexElementAttributes($user);
        }
}
</code></pre>

<plugin-development><usermodel>
Is there a way to programatically add a user when no one is logged in
# Is there a way to programatically add a user when no one is logged in

(Asked by: username_0 on 2016-11-23)

In my controller, I can add new users when a user with the right privileges is logged in via craft()->users->saveUser(), but this doesn't work when no user is logged in.
Is "users/saveUser" the only service method I can use? I would prefer to be able to use my custom one.
EDIT:
code below works fine when I'm logged in as an admin user, otherwise it redirects me to login form, after form submission. Have I missed something in here?
public function actionAddPartner() 
{
        $content = craft()->request->getPost();
        $companyId = explode("@", $content['email']);
        $user = new UserModel();
        $user->username         = $content['email'];
        $user->email            = $content['email'];
        $user->unverifiedEmail  = $content['email'];
        $user->firstName        = $content['firstName'];
        $user->lastName         = $content['lastName'];
        $user->pending          = true;
        $success = craft()->users->saveUser($user);
        if ($success) {
            // Assign them to the Partner Admin user group
            craft()->userGroups->assignUserToGroups($user->id, 1);
            // Update the Element Index
            craft()->search->indexElementAttributes($user);
        }
}

**Tags:** plugin-development, usermodel


---

## 2 answers

### Answer by username_0 on 2016-11-23. Score: 2

Oh well, totally forgot to allow anonymous access at the top of the controller.
I guess I can leave this answer here in case somebody is as forgetful as me:
protected $allowAnonymous = true;

> **Comments:**
> **username_1**: Hah... funny thing is I've done that plenty of times.  Glad you're sorted!  
> **username_0**: yeah, one of those things that can drive you insane, especially if one, like me, is still learning.  

---

### Answer by username_1 on 2016-11-23. Score: 0

[`UsersService->saveUser()`](https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/UsersService.php#L240) (at `craft()->users->saveUser()`) doesn't do any sort of permissions checking or authentication checks because it assumes that all of that has been performed at the controller level.

You'll find this similar interactions between controllers and services elsewhere in Craft as well.

My guess is you're hitting some sort of validation error when trying to save a user with an authenticated session. Try checking the output of `$user->getErrors()`.

You can, of course, in your plugin write your own service method that saves a user in a method that differs from Craft's implementation.

> **Comments:**
> **username_0**: I appreciate your answer, but when I try to register a user while logged out I'm being redirected to a login form. I've updated the question with the actual code. This is the controller to which I direct the registration form.  
> **username_1**: Maybe the example code isn't complete?  Where is `$success` being defined?  

---

