public function testAreValidReturnsOnlyValidPermissions() { $item = new Permission(); // Use "Foo" to act as an invaid ID $permission_ids = array('Foo', 1, 2, 3); $returned = $item->areValid($permission_ids); $this->assertNotContains('Foo', $returned); $this->assertCount(3, $returned); $permission_ids = array(1); $returned = $item->areValid($permission_ids); $this->assertContains(1, $returned); }
/** * Run the migrations. * * @return void */ public function up() { $this->prefix = \Config::get('empower::dbprefix'); // Create series table Schema::create($this->prefix . 'series', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->string('title')->length(100); $table->timestamps(); }); // Create posts table Schema::create($this->prefix . 'posts', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->integer('user_id')->unsigned(); $table->integer('series_id')->unsigned(); $table->smallInteger('series_order')->unsigned()->default(0); $table->string('title')->length(250); $table->string('slug')->length(250); $table->text('content'); $table->boolean('published')->default(0); $table->timestamps(); }); // Create tags table Schema::create($this->prefix . 'tags', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->string('name')->length(50); $table->string('slug')->length(50); $table->timestamps(); }); // Create categories table Schema::create($this->prefix . 'categories', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->string('name')->length(50); $table->string('slug')->length(50); $table->timestamps(); }); // Create post_tag table Schema::create($this->prefix . 'post_tag', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->integer('post_id')->unsigned(); $table->integer('tag_id')->unsigned(); }); // Create category_post table Schema::create($this->prefix . 'category_post', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->integer('category_id')->unsigned(); $table->integer('post_id')->unsigned(); }); // Add permissions $permissions = array('create_posts' => 'Create Blog Posts', 'edit_posts' => 'Edit Blog Posts', 'show_posts' => 'Show Blog Posts', 'destroy_posts' => 'Destroy Blog Posts', 'create_series' => 'Create Blog Series', 'edit_series' => 'Edit Blog Series', 'show_series' => 'Show Blog Series', 'destroy_series' => 'Destroy Blog Series'); foreach ($permissions as $task => $title) { Permission::create(array('task' => $task, 'title' => $title)); } }
/** * Run the migrations. * * @return void */ public function up() { $this->prefix = \Config::get('empower::dbprefix'); // Create permissions tables Schema::create($this->prefix . 'permissions', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->string('title')->length(50); $table->string('task')->length(50); $table->timestamps(); }); // Create roles tables Schema::create($this->prefix . 'roles', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->string('title')->length(50); $table->timestamps(); }); // Create users tables Schema::create($this->prefix . 'users', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->integer('role_id')->unsigned(); $table->string('name')->length(100); $table->string('email')->length(250); $table->string('password')->length(60); $table->timestamps(); }); // Create a permission_role tables Schema::create($this->prefix . 'permission_role', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->integer('permission_id'); $table->integer('role_id')->unsigned(); }); $permissions = array('create_permissions' => 'Create Permissions', 'edit_permissions' => 'Edit Permissions', 'show_permissions' => 'Show Permissions', 'destroy_permissions' => 'Destroy Permissions', 'create_roles' => 'Create Roles', 'edit_roles' => 'Edit Roles', 'show_roles' => 'Show Roles', 'destroy_roles' => 'Destroy Roles', 'create_users' => 'Create Users', 'edit_users' => 'Edit Users', 'show_users' => 'Show Users', 'destroy_users' => 'Destroy Users'); foreach ($permissions as $task => $title) { Permission::create(array('task' => $task, 'title' => $title)); } $role = Role::create(array('title' => Config::get('aurp::godmode'))); $user = new User(); $user->role_id = $role->id; $user->name = 'Admin'; $user->email = '*****@*****.**'; $user->password = '******'; // password is automatically hashed $user->save(); }
/** * Checks if the supplied array of permissions exist within database * * @return array|null */ public static function areValid($permissions) { if (!is_null($permissions)) { // Grab any permissions that are in that array $against = Permission::whereIn('id', $permissions)->get(); // Compare the two counts, if different, some of the ones supplied don't exist if ($against->count() !== count($permissions)) { // in which case, supply new array of ones that do exist and were requested $permissions = array(); foreach ($against as $permission) { $permissions[] = $permission->id; } } return $permissions; } return null; }
public function areValid($permissions) { return Permission::areValid($permissions); }