Jump to content

Creating New users: addRole doesn't work (sometimes)


Rich Torres
 Share

Recommended Posts

I am a bit confused. I have a simple form to add new users, and I damn near have everything working. As a matter of fact it WAS working just fine a few times while testing. I really changed very little. The role "representative" exists yet I still get an error:

Quote

Error: Exception: Method NullPage::addRole does not exist or is not callable in this context



 

<form method="post" action="./">
  <label for="firstName">First Name</label>
  <input name="firstName" type="text" required>
  <label for="lastName">Last Name</label>
  <input name="lastName" type="text" required>
  <label for="email">Email</label>
  <input name="email" type="email" required>
  
  <button name="submit" type="submit">Get Started</button>
</form>

<?php

if (isset($_POST['submit'])) {
        // Puts users in an array, and finds a userID that is not being used
        $agentList = array();
        $number = 1001;

        foreach($users->find('start=0, roles=representative') as $v) {
                array_push($agentList, $v->userID);
        }

        for ($number; ;) {
                if(in_array($number, $agentList)) {
                        $number++;
                } else {
                        $newAgentID = $number;
                        break;
                }
        }
        // Place POST data into variables
        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        $fullName = array($firstName,$lastName);
        $email = $_POST['email'];
      
        //Have Processwire add the new user     
        $u = $users->add($firstName[0].$lastName);
        $u->firstName = $firstName;
        $u->lastName = $lastName;
        $u->pass = "Password";
        $u->addRole('representative');
        $u->email = $email;
        $u->userID = $newAgentID;
        $u->save();

        //Add user to ViciDial
        $viciAdminUser = 10;
        $viciAdminPass = "password";
        $viciUser = $newAgentID;
        $viciUserName = implode(" ",$fullName);
        $viciUserPass = "Password".$newAgentID;  
        $viciUserLevel = 1;
        $viciUserGroup = "Arizona";
}       

 

Link to comment
Share on other sites

The message complains about $u being empty (i.e. a NullPage object), so the call to $users->add didn't succeed. A likely cause is that a user with the name you are trying to add already exists. You should check the return of $users->add, or even better, check if the name in question is already there beforehand and output a message accordingly (and don't forget to sanitize the form input).

There's also a nice API to access GET and POST parameters in ProcessWire. Instead of using e.g. $_POST["submit"], you can access $input->post->submit and don't trigger warnings/errors when that parameter isn't defined.

  • Like 6
Link to comment
Share on other sites

@BitPoet You were dead on! User existed. That was impressive. No wonder is worked some times, but not others. Also, thank you for the API insight.

@dragan The purpose of the for is to loop through existing user ID #s to find one that does not exist. I am not sure if my method was the best, but it works.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

I have to reopen this thread, because I still have these problems.

I am absolutely sure that my user-name is valid and unique.
Still, if using $users->add($name) I'm getting a NullPage back, but just sometimes.
I couldn't find a way to reproduce this error on our test-server.

Our database is huge, we have some traffic and probably sometimes users are created nearly simultaneously (but definitely with unique names)
Currently we have more than 150k user-accounts in the system.
Do you think that could be a problem?

The solution of @Noel Boss is interesting. How will the user-name be generated in this case?

Any help is really appreciated! Thank you!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...