Skip to content

Commit

Permalink
Removed user_metas table and any dependencies on this table. Moved fi…
Browse files Browse the repository at this point in the history
…elds into the users table. Other small tweaks.
  • Loading branch information
mmanos committed Jan 2, 2015
1 parent 0924014 commit 6e2cf12
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 271 deletions.
10 changes: 3 additions & 7 deletions src/AuthorizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AuthorizeCommand extends \Symfony\Component\Console\Command\Command
protected function configure()
{
$this->setName('authorize')
->setDescription('Add authorization support to an existing Laravle application');
->setDescription('Add authorization support to an existing Laravel application');
}

/**
Expand Down Expand Up @@ -54,8 +54,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
protected function createAuthModels($directory, $output)
{
File::copyIfNone(
dirname(__FILE__).'/../stubs/app/database/migrations/2014_09_02_000000_create_authorize_tables.stub',
$directory.'/app/database/migrations/2014_09_02_000000_create_authorize_tables.php'
dirname(__FILE__).'/../stubs/app/database/migrations/2015_01_01_000000_create_authorize_tables.stub',
$directory.'/app/database/migrations/2015_01_01_000000_create_authorize_tables.php'
);
File::copy(
dirname(__FILE__).'/../stubs/app/models/User.stub',
Expand All @@ -65,10 +65,6 @@ protected function createAuthModels($directory, $output)
dirname(__FILE__).'/../stubs/app/models/Role.stub',
$directory.'/app/models/Role.php'
);
File::copyIfNone(
dirname(__FILE__).'/../stubs/app/models/User/Meta.stub',
$directory.'/app/models/User/Meta.php'
);
File::copyIfNone(
dirname(__FILE__).'/../stubs/app/models/Service/User.stub',
$directory.'/app/models/Service/User.php'
Expand Down
2 changes: 1 addition & 1 deletion src/FrontendCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function installCasset($directory, $output)
File::replaceOnce(
$directory.'/app/config/app.php',
"=> 'Illuminate\Support\Facades\View',",
"=> 'Illuminate\Support\Facades\View',\n\t\t'Casset' => 'Mmanos\Casset\Facades\Casset',"
"=> 'Illuminate\Support\Facades\View',\n\t\t'Casset' => 'Mmanos\Casset\Facades\Casset',"
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,34 @@ class CreateAuthorizeTables extends Migration
$table->increments('id');
$table->string('email');
$table->string('password');
$table->string('remember_token')->nullable();
$table->string('permissions');
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->timestamps();
$table->softDeletes();
$table->string('remember_token')->nullable();
$table->string('auth_reset_token')->nullable();
$table->timestamp('reset_token_expires_at')->nullable();

$table->unique('email');
$table->index(array('created_at', 'deleted_at', 'permissions'), 'recent_users');
});

Schema::create('user_metas', function ($table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('name');
$table->text('value')->nullable();
$table->timestamps();

$table->unique(array('user_id', 'name'));
$table->index('name');
$table->unique('email', 'idx_email');
$table->index('auth_reset_token', 'idx_auth_reset_token');
$table->index(array('created_at', 'deleted_at'), 'recent_users');
$table->index(array('first_name', 'deleted_at', 'created_at'), 'alpha_users');
});

Schema::create('roles', function ($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();

$table->unique('name');
$table->unique('name', 'idx_name');
});

Schema::create('user_roles', function ($table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->timestamps();

$table->unique(array('user_id', 'role_id'));
$table->unique(array('user_id', 'role_id'), 'user_roles');
});

DB::table('roles')->insertGetId(array(
Expand All @@ -72,7 +66,6 @@ class CreateAuthorizeTables extends Migration
public function down()
{
Schema::drop('users');
Schema::drop('user_metas');
Schema::drop('roles');
Schema::drop('user_roles');
}
Expand Down
14 changes: 0 additions & 14 deletions stubs/app/models/Role.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@

class Role extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'roles';

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('updated_at');

/**
* The attributes that aren't mass assignable.
*
Expand Down
83 changes: 18 additions & 65 deletions stubs/app/models/Service/User.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Service;

use Event, Hash, Str;
use Event, Hash;

/**
* The User service.
Expand All @@ -15,50 +15,30 @@ class User
* @param string $first_name First name.
* @param string $last_name Last name.
* @param array $options Array of optional parameters:
* - permissions
* - enc_password
* - metas
* - roles
*
* @return \User
*/
public static function create($email, $password, $first_name, $last_name, array $options = array())
{
if (empty($options['metas']) || !is_array($options['metas'])) {
$options['metas'] = array();
}

$user = new \User(array(
'email' => $email,
'password' => Hash::make($password),
'permissions' => 'public',
'email' => $email,
'password' => Hash::make($password),
'first_name' => $first_name,
'last_name' => $last_name,

));

if (!empty($options['permissions'])) {
$user->permissions = $options['permissions'];
}

if (!empty($options['enc_password'])) {
$user->password = $options['enc_password'];
}

$user->save();

$options['metas']['first_name'] = $first_name;
$options['metas']['last_name'] = $last_name;

foreach ($options['metas'] as $name => $value) {
$user->metas()->save(new \User\Meta(array(
'name' => $name,
'value' => $value,
)));
}

if (!empty($options['roles']) && is_array($options['roles'])) {
foreach ($options['roles'] as $role) {
$user->roles()->attach(
\Role::where('name', $role)->first()
);
$user->roles()->attach(\Role::where('name', $role)->first());
}
}

Expand All @@ -75,9 +55,7 @@ class User
* - email
* - first_name
* - last_name
* - permissions
* - password
* - metas
* - remove_roles
* - add_roles
*
Expand All @@ -88,45 +66,20 @@ class User
if (!empty($options['email'])) {
$user->email = $options['email'];
}

if (!empty($options['permissions'])) {
$user->permissions = $options['permissions'];
if (!empty($options['first_name'])) {
$user->first_name = $options['first_name'];
}
if (!empty($options['last_name'])) {
$user->last_name = $options['last_name'];
}

if (!empty($options['password'])) {
$user->password = Hash::make($options['password']);
}

$user->save();

if (!empty($options['first_name'])) {
$options['metas']['first_name'] = $options['first_name'];
}
if (!empty($options['last_name'])) {
$options['metas']['last_name'] = $options['last_name'];
if (!empty($options['enc_password'])) {
$user->password = $options['enc_password'];
}

if (!empty($options['metas']) && is_array($options['metas'])) {
foreach ($options['metas'] as $name => $value) {
if (null === $value) {
if ($meta = $user->metas()->where('name', $name)->first()) {
$meta->delete();
}

continue;
}

if (!$meta = $user->metas()->where('name', $name)->first()) {
$meta = new \User\Meta(array(
'user_id' => $user->id,
'name' => $name,
));
}

$meta->value = $value;
$meta->save();
}
}
$user->save();

if (!empty($options['remove_roles']) && is_array($options['remove_roles'])) {
foreach ($options['remove_roles'] as $remove) {
Expand All @@ -142,9 +95,7 @@ class User
continue;
}

$user->roles()->attach(
\Role::where('name', $add)->first()
);
$user->roles()->attach(\Role::where('name', $add)->first());
}
}

Expand All @@ -160,6 +111,8 @@ class User
*/
public static function delete(\User $user)
{
$user->email = $user->email . '-DELETED-' . microtime(true);
$user->save();
$user->delete();

Event::fire('user.delete', array($user));
Expand Down
40 changes: 14 additions & 26 deletions stubs/app/models/Service/User/Auth.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Service\User;

use Event, Str, Config;
use Event, Str, Config, Hash;

/**
* The User Auth service.
Expand All @@ -18,15 +18,12 @@ class Auth
{
$token = Str::random(20);

if (!$meta = $user->metas()->where('name', 'auth_reset_token')->first()) {
$meta = new \User\Meta(array(
'user_id' => $user->id,
'name' => 'auth_reset_token',
));
}

$meta->value = $token;
$meta->save();
$user->auth_reset_token = $token;
$user->reset_token_expires_at = date(
'Y-m-d H:i:s',
time() + Config::get('auth.reset_token_expires', 604800)
);
$user->save();

Event::fire('user.auth.forgotPassword', array($user, $token));

Expand All @@ -42,20 +39,13 @@ class Auth
*/
public static function userFromToken($token)
{
$meta = \User\Meta::where('name', 'auth_reset_token')
->where('value', $token)
->first();

if (!$meta) {
return false;
}
$user = \User::where('auth_reset_token', $token)->first();

$expires = Config::get('auth.reset_token_expires', 604800);
if (time() - strtotime($meta->created_at) >= $expires) {
if (!$user) {
return false;
}

if (!$user = \User::find($meta->user_id)) {
if (time() >= strtotime($user->reset_token_expires_at)) {
return false;
}

Expand All @@ -73,12 +63,10 @@ class Auth
*/
public static function reset(\User $user, $password)
{
\Service\User::update($user, array(
'password' => $password,
'metas' => array(
'auth_reset_token' => null,
),
));
$user->password = Hash::make($password);
$user->auth_reset_token = null;
$user->reset_token_expires_at = null;
$user->save();

Event::fire('user.auth.reset', array($user, $password));
}
Expand Down
Loading

0 comments on commit 6e2cf12

Please sign in to comment.