I'd like to add admin privileges to a user I'm creating like so:

  if($user == null)
    $firstName = $_GET['firstName'];

    $lastName = $_GET['lastName'];
    $userName = $firstName . $lastName;
    $user = new UserModel();

    $user->username   = $userName;
    $user->email      = $email;
    $user->firstName  = $firstName;
    $user->lastName   = $lastName;


    $permissions = [HOW DO I SET ADMIN PERMS?]
    craft()->userPermissions->saveUserPermissions($user['id'], $permissions)

  • 2
    I don't know the context that you're doing this in, but needless to say, generally speaking be cautious in creating admins programatically. Especially if it's a front-end request from untrusted input. – Brad Bell Mar 17 '17 at 0:04

This should do the trick...

$user->admin = true;

That data is part of the UserModel, so be sure to add it before the saveUser call.

Being a full-blown admin not technically part of setting permissions. If the user is an admin, that supersedes all other permissions settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.