2

Bit of an awkward one.

The client wants a system where they send us a link e.g. mycraftsite.com?userid=abc123 and from there we need to populate the user table.

I am new to craft but I am assuming the correct way is to use a plugin, grab the header data and somehow insert it using a hook, but I don't see any available for before the site renders.

Can anyone help point me in the right direction?

1
  • Are the passing in the username and email through the link? How is the password supposed to get set?
    – Brad Bell
    Feb 16 '15 at 18:04
4

You would most likely want to add a controller to your custom plugin, with an 'action' method designed to handle the url request and generate a user; using 'post' or 'get' to pass parameters to the controller action.

In the controller action, you can access your parameters using:

$username = craft()->request->getParam('username');

You can then populate a new User Model using the passed params and save the model.

$user = new UserModel();

$user->username   = $username;
$user->email      = $email;
$user->firstName  = $firstName;
$user->lastName   = $lastName;
$user->getContent()->setAttributes([ 
    'customField' => $customFieldValue
]);

craft()->users->saveUser($user);

// We now have a user Id, so assign user to a group
craft()->userGroups->assignUserToGroups($user->id, $groupId);

// or to multiple groups
//craft()->userGroups->assignUserToGroups($user->id, [$groupId, $otherGroupId]);

However, by default, controller actions are only accessible to logged-in users. If you want to allow anonymous access you would need to specifically allow it using the $allowAnonymous property. This definitely introduces some security issues however, so proceed with caution. For example: definitely do not just assign user group(s) blindly from passed parameters or someone might be able to give themselves admin access!

2
  • That is exactly what I was looking for, thanks! can you recommend the best way of hooking it in? The client will provide this link to be used on initial site load, so I need to hook it in before the site renders initially.
    – ConquestXD
    Feb 16 '15 at 21:47
  • It will be enabled as soon as you activate your plugin — so create plugin; place in plugins folder; goto to plugin settings, install and activate. Check out the plugin docs for more info on setting up, installing plugins, etc and/or start with a plugin template like the BusinessLogic Template plugin. Feb 16 '15 at 22:14

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.