Created
March 13, 2012 03:03
-
-
Save dberry37388/2026310 to your computer and use it in GitHub Desktop.
Sentry Task to Create an Admin User
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Part of the Sentry package for FuelPHP. | |
* | |
* @package Sentry | |
* @version 2.0 | |
* @author Cartalyst LLC | |
* @license MIT License | |
* @copyright 2011 Cartalyst LLC | |
* @link http://cartalyst.com | |
*/ | |
namespace Fuel\Tasks; | |
use Cli; | |
use Package; | |
use Sentry; | |
use Str; | |
/** | |
* Sentry task to create a default admin user | |
* | |
* @author Daniel Berry | |
* @package Sentry | |
*/ | |
Class Createadmin | |
{ | |
public static function run() | |
{ | |
Package::load('sentry'); | |
if (!$group = self::create_group()) | |
{ | |
self::create_group(); | |
} | |
elseif(!$user_id = self::create_user()) | |
{ | |
self::create_user(); | |
} | |
elseif(!$group = self::add_to_group($user_id, $group)) | |
{ | |
self::add_to_group($user_id, $group); | |
} | |
} | |
public static function create_group() | |
{ | |
// First let's create the admin group | |
$admin_group = Str::lower(Cli::prompt('Name of Admin Group?', 'superuser')); | |
// check to see the if the group already exists | |
if (!Sentry::group_exists($admin_group)) | |
{ | |
try | |
{ | |
$group_id = Sentry::group()->create(array( | |
'name' => $admin_group, | |
)); | |
if ($group_id) | |
{ | |
Sentry::group($admin_group)->update_permissions(array($admin_group => 1)); | |
Cli::write('Admin Group ('.$admin_group.') has been successfully created.', 'green'); | |
return $admin_group; | |
} | |
} | |
catch (SentryGroupException $e) | |
{ | |
Cli::write($e->getMessage(), 'red'); | |
} | |
return false; | |
} | |
return true; | |
} | |
public static function create_user() | |
{ | |
Package::load('sentry'); | |
/** | |
* Let's create the admin user. Prompt for details | |
*/ | |
$email = Cli::prompt('Email Address for admin user'); | |
$password = Cli::prompt('Create a password for the admin user.', Str::random('alnum', 8)); | |
$username = Cli::prompt('Select a username for the admin user.', 'admin'); | |
$first_name = Cli::prompt('What is the admin user\'s First Name?'); | |
$last_name = Cli::prompt('What is the admin user\'s Last Name?'); | |
try | |
{ | |
$vars = array( | |
'email' => $email, | |
'password' => $password, | |
'username' => $username, | |
'metadata' => array( | |
'first_name' => $first_name, | |
'last_name' => $last_name | |
) | |
); | |
$user_id = Sentry::user()->create($vars); | |
if ($user_id) | |
{ | |
Cli::write('Default admin user named '.$username.' has been successfully created.', 'green'); | |
return $user_id; | |
} | |
else | |
{ | |
Cli::write('An error occured while creating the account.', 'red'); | |
} | |
} | |
catch (SentryUserException $e) | |
{ | |
Cli::write($e->getMessage(), 'red'); | |
} | |
return false; | |
} | |
public static function add_to_group($user_id, $group) | |
{ | |
/** | |
* Add the admin user to the admin group | |
*/ | |
try | |
{ | |
// update the user | |
$user = Sentry::user($user_id); | |
// add to group specified above in $admin_group | |
$user->add_to_group($group); | |
Cli::write('User has been added to the '.$group.' group.', 'green'); | |
return true; | |
} | |
catch (SentryUserException $e) | |
{ | |
Cli::write($e->getMessage(), 'red'); | |
} | |
return false; | |
} | |
} | |
/** end of tasks/createadmin.php **/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment