Skip to content

Instantly share code, notes, and snippets.

@engram-design
Last active April 16, 2020 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save engram-design/6dba6a0ec19ff38727d3 to your computer and use it in GitHub Desktop.
Save engram-design/6dba6a0ec19ff38727d3 to your computer and use it in GitHub Desktop.
Custom UserController
<?php
namespace Craft;
class CustomUserDomainPlugin extends BasePlugin
{
/* --------------------------------------------------------------
* PLUGIN INFO
* ------------------------------------------------------------ */
public function getName()
{
return Craft::t('Custom User Registration');
}
public function getVersion()
{
return '1.0';
}
public function getDeveloper()
{
return 'Josh Crawford';
}
public function getDeveloperUrl()
{
return 'http://sgroup.com.au';
}
public function init()
{
parent::init();
craft()->on('users.onBeforeSaveUser', function(Event $event) {
$user = $event->params['user'];
$email = craft()->request->getPost('email', $user->email);
$valid = true;
if (!checkEmailIsAllowedDomain($email)) {
$user->addError('email', Craft::t('Email supplied is not from allowed domain.'));
$valid = false;
}
if (!$valid) {
$event->performAction = false;
craft()->urlManager->setRouteVariables(array(
'account' => $user
));
}
});
}
}
{% macro errorList(errors) %}
{% if errors %}
{% for error in errors %}
<label class="error">{{ error }}</label>
{% endfor %}
{% endif %}
{% endmacro %}
{% from _self import errorList %}
<form method="post" accept-charset="UTF-8">
{{ getCsrfInput() }}
<input type="hidden" name="action" value="users/saveUser">
<input type="hidden" name="redirect" value="">
<input placeholder="Email" type="email" name="email" required="required">
{% if account is defined %}
{{ errorList(account.getErrors('email')) }}
{% endif %}
</form>
@engram-design
Copy link
Author

Create a directory called customuserdomain (all lowercase), and add the CustomUserDomainPlugin.php to it. Use the register form as normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment