fix conflict

This commit is contained in:
Nazri
2018-05-14 10:45:15 +08:00
6780 changed files with 35109 additions and 708041 deletions
+3 -1
View File
@@ -14,10 +14,12 @@ use Faker\Generator as Faker;
*/
$factory->define(App\User::class, function (Faker $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});
@@ -0,0 +1,41 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOauthProvidersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_providers', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('provider');
$table->string('provider_user_id')->index();
$table->string('access_token')->nullable();
$table->string('refresh_token')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('oauth_providers');
}
}
@@ -0,0 +1,75 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class EntrustSetupTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::beginTransaction();
// Create table for storing roles
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
// Create table for associating roles to users (Many-to-Many)
Schema::create('role_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['user_id', 'role_id']);
});
// Create table for storing permissions
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
// Create table for associating permissions to roles (Many-to-Many)
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
DB::commit();
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('permission_role');
Schema::drop('permissions');
Schema::drop('role_user');
Schema::drop('roles');
}
}
+4 -1
View File
@@ -11,6 +11,9 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(UsersTableSeeder::class);
$this->call(RolesAndPermissionsSeeder::class);
}
}
@@ -0,0 +1,189 @@
<?php
/*
|--------------------------------------------------------------------------
| Roles & Permissions Seeder
|--------------------------------------------------------------------------
|
| This Seeder class allows you to update and create Roles & Permissions
| for the Laravel Entrust package.
|
| USE -> php artisan db:seed --class=RolesAndPermissionsSeeder
|
| https://github.com/thomasfw/RolesAndPermissionsSeeder
|
|--------------------------------------------------------------------------
| Make sure you update the namespaces for your User & Entrust models
|--------------------------------------------------------------------------
*/
use App\User as User;
use App\Role as Role;
use App\Permission as Permission;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class RolesAndPermissionsSeeder extends Seeder {
protected $roles = [
'admin' => [
],
'member' => [
],
];
protected $permissions = [
// Add your Permissions here
];
/**
* Roles
*
* @return array()
*/
public function roles()
{
return $this->roles;
}
/**
* Permissions
*
* @param $name
* @return array()
*/
public function permissions($name = '')
{
$single = (array_key_exists($name,$this->permissions) ? array($name =>$this->permissions[$name]) : false );
return ($name ? $single : $this->permissions);
}
/**
* Run the Seeder
*
* @return void
*/
public function run()
{
DB::table(Config::get('entrust.permissions_table'))->delete();
foreach ($this->roles() as $key => $val) {
$this->command->info(" ");
$this->command->info('Creating/updating the \''.$key.'\' role');
$this->command->info('-----------------------------------------');
$val['name'] = $key;
$this->reset($val);
}
$this->cleanup();
}
/**
* Reset Role, Permissions & Users
*
* @param $role
* @return void
*/
public function reset($role)
{
$commandBullet = ' -> ';
// The Old Role
$originalRole = Role::where('name',$role['name'])->first();
if($originalRole) Role::where('id',$originalRole->id)->update(['name' => $role['name'].'__remove']);
// The New Role
$newRole = new Role();
$newRole->name = $role['name'];
if(isset($role['display_name'])) $newRole->display_name = $role['display_name']; // optional
if(isset($role['description'])) $newRole->description = $role['description']; // optional
$newRole->save();
$this->command->info($commandBullet."Created $role[name] role");
// Set the Permissions (if they exist)
$pcount = 0;
if(!empty($role['permissions']))
{
foreach ($role['permissions'] as $permission_name) {
$permission = $this->permissions($permission_name);
if($permission === false || (!$permission_name)) {
$this->command->error($commandBullet."Failed to attach permission '$permission_name'. It does not exist");
continue;
}
$newPermission = \Permission::where('name',$permission_name)->first();
if (!$newPermission) {
$newPermission = new Permission();
$newPermission->name = key($permission);
if(isset($permission['display_name'])) $newPermission->display_name = $permission['display_name']; // optional
if(isset($permission['description'])) $newPermission->description = $permission['description']; // optional
$newPermission->save();
}
$newRole->attachPermission($newPermission);
$pcount++;
}
}
$this->command->info($commandBullet."Attached $pcount permissions to $role[name] role");
// Update old records
if ($originalRole)
{
$userCount = 0;
$RoleUsers = DB::table(Config::get('entrust.role_user_table'))->where('role_id',$originalRole->id)->get();
foreach ($RoleUsers as $user) {
$u = User::where('id',$user->user_id)->first();
$u->attachRole($newRole);
$userCount++;
}
$this->command->info($commandBullet."Updated role attachment for $userCount users");
Role::where('id',$originalRole->id)->delete(); // will also remove old role_user records
$this->command->info($commandBullet."Removed the original $role[name] role");
}
}
/**
* Cleanup()
* Remove any roles & permissions that have been removed
* @return void
*/
public function cleanup()
{
$commandBullet = ' -> ';
$this->command->info(" ");
$this->command->info('Cleaning up roles & permissions:');
$this->command->info('--------------------------------');
$storedRoles = Role::all();
if(!empty($storedRoles)) {
$definedRoles = $this->roles();
foreach ($storedRoles as $role) {
if ( !array_key_exists($role->name,$definedRoles) ) {
Role::where('name',$role->name)->delete();
$this->command->info($commandBullet.'The \''.$role->name.'\' role was removed');
}
}
}
$storedPerms = DB::table(Config::get('entrust.permissions_table'))->get();
if(!empty($storedPerms)) {
$definedPerms = $this->permissions();
foreach ($storedPerms as $perm) {
if ( !array_key_exists($perm->name,$definedPerms) ) {
DB::table(Config::get('entrust.permissions_table'))->where('name',$perm->name)->delete();
$this->command->info($commandBullet.'The \''.$perm->name.'\' permission was removed');
}
}
}
$this->command->info($commandBullet.'Done');
$this->command->info(" ");
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Seeder;
use App\User;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->delete();
$users = array(
['name' => 'Ryan Chenkie', 'email' => 'ryanchenkie@gmail.com', 'password' => Hash::make('secret')],
['name' => 'Chris Sevilleja', 'email' => 'chris@scotch.io', 'password' => Hash::make('secret')],
['name' => 'Holly Lloyd', 'email' => 'holly@scotch.io', 'password' => Hash::make('secret')],
['name' => 'Adnan Kukic', 'email' => 'adnan@scotch.io', 'password' => Hash::make('secret')],
);
foreach ($users as $user)
{
User::create($user);
}
}
}