From 6e2cf12fc85510c755d8c89831b35b80e6a00bcb Mon Sep 17 00:00:00 2001 From: Mark Manos Date: Fri, 2 Jan 2015 15:40:41 -0600 Subject: [PATCH] Removed user_metas table and any dependencies on this table. Moved fields into the users table. Other small tweaks. --- src/AuthorizeCommand.php | 10 +- src/FrontendCommand.php | 2 +- ...01_01_000000_create_authorize_tables.stub} | 29 +++--- stubs/app/models/Role.stub | 14 --- stubs/app/models/Service/User.stub | 83 ++++------------ stubs/app/models/Service/User/Auth.stub | 40 +++----- stubs/app/models/User.stub | 96 +++---------------- stubs/app/models/User/Meta.stub | 37 ------- stubs/app/models/Validator/User.stub | 18 ++-- stubs/app/views/auth/forgot.blade.stub | 2 +- stubs/app/views/auth/login.blade.stub | 2 +- stubs/app/views/auth/reset.blade.stub | 2 +- stubs/app/views/index/index.blade.stub | 3 +- .../views/layouts/default/navbar.blade.stub | 7 +- stubs/app/views/signup/index.blade.stub | 2 +- .../assets/css/layouts/default/bootstrap.stub | 2 +- 16 files changed, 78 insertions(+), 271 deletions(-) rename stubs/app/database/migrations/{2014_09_02_000000_create_authorize_tables.stub => 2015_01_01_000000_create_authorize_tables.stub} (69%) delete mode 100644 stubs/app/models/User/Meta.stub diff --git a/src/AuthorizeCommand.php b/src/AuthorizeCommand.php index e986841..0a33b76 100644 --- a/src/AuthorizeCommand.php +++ b/src/AuthorizeCommand.php @@ -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'); } /** @@ -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', @@ -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' diff --git a/src/FrontendCommand.php b/src/FrontendCommand.php index 907a380..22d3038 100644 --- a/src/FrontendCommand.php +++ b/src/FrontendCommand.php @@ -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'," ); } diff --git a/stubs/app/database/migrations/2014_09_02_000000_create_authorize_tables.stub b/stubs/app/database/migrations/2015_01_01_000000_create_authorize_tables.stub similarity index 69% rename from stubs/app/database/migrations/2014_09_02_000000_create_authorize_tables.stub rename to stubs/app/database/migrations/2015_01_01_000000_create_authorize_tables.stub index 1598aff..2be35f9 100644 --- a/stubs/app/database/migrations/2014_09_02_000000_create_authorize_tables.stub +++ b/stubs/app/database/migrations/2015_01_01_000000_create_authorize_tables.stub @@ -16,24 +16,18 @@ 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) { @@ -41,7 +35,7 @@ class CreateAuthorizeTables extends Migration $table->string('name'); $table->timestamps(); - $table->unique('name'); + $table->unique('name', 'idx_name'); }); Schema::create('user_roles', function ($table) { @@ -49,7 +43,7 @@ class CreateAuthorizeTables extends Migration $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( @@ -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'); } diff --git a/stubs/app/models/Role.stub b/stubs/app/models/Role.stub index 860d573..c1b63b4 100644 --- a/stubs/app/models/Role.stub +++ b/stubs/app/models/Role.stub @@ -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. * diff --git a/stubs/app/models/Service/User.stub b/stubs/app/models/Service/User.stub index 47a0f3b..dcef499 100644 --- a/stubs/app/models/Service/User.stub +++ b/stubs/app/models/Service/User.stub @@ -1,6 +1,6 @@ $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()); } } @@ -75,9 +55,7 @@ class User * - email * - first_name * - last_name - * - permissions * - password - * - metas * - remove_roles * - add_roles * @@ -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) { @@ -142,9 +95,7 @@ class User continue; } - $user->roles()->attach( - \Role::where('name', $add)->first() - ); + $user->roles()->attach(\Role::where('name', $add)->first()); } } @@ -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)); diff --git a/stubs/app/models/Service/User/Auth.stub b/stubs/app/models/Service/User/Auth.stub index 27bcf0d..46c66b1 100644 --- a/stubs/app/models/Service/User/Auth.stub +++ b/stubs/app/models/Service/User/Auth.stub @@ -1,6 +1,6 @@ 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)); @@ -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; } @@ -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)); } diff --git a/stubs/app/models/User.stub b/stubs/app/models/User.stub index 2e20b7c..b4b7d66 100644 --- a/stubs/app/models/User.stub +++ b/stubs/app/models/User.stub @@ -7,26 +7,19 @@ class User extends Model implements UserInterface { use UserTrait, SoftDeletingTrait; - /** - * The database table used by the model. - * - * @var string - */ - protected $table = 'users'; - /** * The attributes excluded from the model's JSON form. * * @var array */ - protected $hidden = array('password', 'updated_at', 'deleted_at', 'remember_token', 'pivot', 'roles', 'metas'); + protected $hidden = array('password', 'deleted_at', 'remember_token', 'auth_reset_token', 'reset_token_expires_at', 'pivot', 'roles'); /** * The accessors to append to the model's array form. * * @var array */ - protected $appends = array('first_name', 'last_name', 'avatar_url'); + protected $appends = array('avatar_url'); /** * The attributes that aren't mass assignable. @@ -36,61 +29,20 @@ class User extends Model implements UserInterface protected $guarded = array('id'); /** - * Return the roles this user belongs to. + * The dates array. * - * @var Collection + * @var array */ - public function roles() - { - return $this->belongsToMany('Role', 'user_roles')->withTimestamps(); - } + protected $dates = array('deleted_at'); /** - * Return the metas associated with this user. + * Return the roles this user belongs to. * * @var Collection */ - public function metas() - { - return $this->hasMany('User\Meta'); - } - - /** - * Returns a piece of meta information for this user. - * $default, if not found. - * - * @param string $name Meta name. - * @param mixed $default Default value. - * - * @return boolean - */ - public function meta($name, $default = null) - { - $metas = $this->metas->filter(function ($meta) use ($name) { - return ($meta->name == $name); - }); - - return $metas->isEmpty() ? $default : $metas->first()->value; - } - - /** - * Convenience method to get the first name for this user. - * - * @return string - */ - public function getFirstNameAttribute() - { - return $this->meta('first_name'); - } - - /** - * Convenience method to get the last name for this user. - * - * @return string - */ - public function getLastNameAttribute() + public function roles() { - return $this->meta('last_name'); + return $this->belongsToMany('Role', 'user_roles')->withTimestamps(); } /** @@ -133,34 +85,14 @@ class User extends Model implements UserInterface } /** - * Convert the model instance to an array. + * Returns true if this user has the given role. * - * @return array + * @var string $role + * + * @return bool */ - public function toArray() + public function hasRole($role) { - if (!Site::wantsApi()) { - return parent::toArray(); - } - - // Handle any preloading. - $output = (array) Input::get('output', array()); - if (in_array('user:metas', $output)) { - $this->metas; - } - - $arr = parent::toArray(); - - // Are we allowed to display the email address? - if (!Acl::allowed($this, 'edit')) { - unset($arr['email']); - } - - // Handle any additional output. - if (in_array('user:roles', $output)) { - $arr['roles'] = $this->user_roles; - } - - return $arr; + return in_array($role, $this->user_roles); } } diff --git a/stubs/app/models/User/Meta.stub b/stubs/app/models/User/Meta.stub deleted file mode 100644 index a0b05ec..0000000 --- a/stubs/app/models/User/Meta.stub +++ /dev/null @@ -1,37 +0,0 @@ -belongsTo('User'); - } -} diff --git a/stubs/app/models/Validator/User.stub b/stubs/app/models/Validator/User.stub index 6e26f5a..004d8dd 100644 --- a/stubs/app/models/Validator/User.stub +++ b/stubs/app/models/Validator/User.stub @@ -28,11 +28,10 @@ class User static::registerEmailAvailable(); $rules = array_merge($rules, array( - 'first_name' => 'required|alpha_dash', - 'last_name' => 'required|alpha_dash', - 'email' => 'required|email|email_available', - 'password' => 'required|min:8', - 'permissions' => 'in:public,private', + 'first_name' => 'required|alpha_dash', + 'last_name' => 'required|alpha_dash', + 'email' => 'required|email|email_available', + 'password' => 'required|min:8', )); return Validator::make($input, $rules, array_merge(self::$messages, $messages)); @@ -48,10 +47,9 @@ class User static::registerEmailAvailable($user); $rules = array_merge($rules, array( - 'first_name' => 'alpha_dash', - 'last_name' => 'alpha_dash', - 'email' => 'email|email_available', - 'permissions' => 'in:public,private', + 'first_name' => 'alpha_dash', + 'last_name' => 'alpha_dash', + 'email' => 'email|email_available', )); return Validator::make($input, $rules, array_merge(self::$messages, $messages)); @@ -128,7 +126,7 @@ class User return false; } - if (Hash::check($value, \Auth::user()->getAuthPassword())) { + if (Hash::check($value, Auth::user()->getAuthPassword())) { return true; } diff --git a/stubs/app/views/auth/forgot.blade.stub b/stubs/app/views/auth/forgot.blade.stub index 0b6b2a7..860870b 100644 --- a/stubs/app/views/auth/forgot.blade.stub +++ b/stubs/app/views/auth/forgot.blade.stub @@ -4,7 +4,7 @@ @section('content')
-
+
@include('common.alerts')

Reset Your Password

diff --git a/stubs/app/views/auth/login.blade.stub b/stubs/app/views/auth/login.blade.stub index ec72bfa..04563b3 100644 --- a/stubs/app/views/auth/login.blade.stub +++ b/stubs/app/views/auth/login.blade.stub @@ -4,7 +4,7 @@ @section('content')
-
+
@include('common.alerts')

Log in to {{ company() }}

diff --git a/stubs/app/views/auth/reset.blade.stub b/stubs/app/views/auth/reset.blade.stub index 2a8f3f3..8249d8d 100644 --- a/stubs/app/views/auth/reset.blade.stub +++ b/stubs/app/views/auth/reset.blade.stub @@ -4,7 +4,7 @@ @section('content')
-
+
@include('common.alerts')

Change Your Password

diff --git a/stubs/app/views/index/index.blade.stub b/stubs/app/views/index/index.blade.stub index 7022d2d..debca0e 100644 --- a/stubs/app/views/index/index.blade.stub +++ b/stubs/app/views/index/index.blade.stub @@ -6,7 +6,6 @@ @section('content')
- - Hello Laravel! + Welcome!
@stop diff --git a/stubs/app/views/layouts/default/navbar.blade.stub b/stubs/app/views/layouts/default/navbar.blade.stub index 44609c2..ebd6eba 100644 --- a/stubs/app/views/layouts/default/navbar.blade.stub +++ b/stubs/app/views/layouts/default/navbar.blade.stub @@ -9,11 +9,10 @@