Last active
August 29, 2015 13:57
-
-
Save clouddueling/9867146 to your computer and use it in GitHub Desktop.
Permissions
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 | |
return [ | |
"account.webpage", | |
"account.resources", | |
"account.resources.create", | |
"account.resources.byte_limit", | |
"account.products", | |
"account.products.social_network", | |
"account.products.duplicator", | |
"account.reports", | |
"account.reports.accountability", | |
"account.reports.goals", | |
"account.reports.leaderboards", | |
"profile.user.metrics", | |
"rmc.access", | |
"rmc.accountability", | |
"rmc.calls", | |
"rmc.tasks", | |
"rmc.services", | |
"rmc.forms", | |
"rmc.columns", | |
"rmc.systems", | |
"rmc.emails", | |
"rmc.email_limit", | |
"rmc.texts", | |
"rmc.text_limit", | |
"rmc.notes", | |
"rmc.checkin", | |
"lmc.access", | |
"builders.access", | |
"builders.rmc.access", | |
"builders.rmc.create", | |
"builders.rmc.call_templates", | |
"builders.rmc.email_templates", | |
"builders.rmc.text_templates", | |
"builders.rmc.systems", | |
"builders.rmc.forms", | |
"builders.lmc.access", | |
"builders.lmc.create", | |
"builders.lmc.3rdpartyforms", | |
"builders.lmc.phonescripts", | |
"builders.lmc.system_centers", | |
"builders.promote.access", | |
"builders.promote.create", | |
"builders.promote.eventblasts", | |
"builders.promote.supplies", | |
"builders.promote.images", | |
"builders.broadcasts.access", | |
"builders.broadcasts.create", | |
"builders.broadcasts.emailblasts", | |
"builders.broadcasts.phoneblasts", | |
"builders.broadcasts.textblasts", | |
"builders.product.access", | |
"builders.product.create", | |
"builders.product.faqs", | |
"builders.product.quizzes", | |
"builders.product.product_systems", | |
"builders.product.product_forms", | |
"broadcasters.access", | |
"broadcasters.emailblasts", | |
"broadcasters.phoneblasts", | |
"broadcasters.textblasts", | |
"broadcasters.eventblasts", | |
"promote.access", | |
"promote.events" | |
]; |
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 | |
namespace Modules\User; | |
class Permissions | |
{ | |
public $user; | |
public $permissions; | |
public function __construct(&$user = false) | |
{ | |
$this->user = $user === false ? \Auth::user() : $user; | |
$this->permissions = $this->user->permissions; | |
$this->permissions = json_decode($this->user->permissions, true); | |
} | |
public function has($request) | |
{ | |
return isset($this->permissions[$request]) ? $this->permissions[$request] : false; | |
} | |
public function set($request, $value) | |
{ | |
if (! is_array($request)) { | |
$request = [$request]; | |
} | |
foreach ($request as $perm) { | |
$this->permissions[$perm] = $value; | |
} | |
$this->save(); | |
return $this; | |
} | |
public function get($request) | |
{ | |
return isset($this->permissions[$request]) ? $this->permissions[$request] : false; | |
} | |
public function save() | |
{ | |
$permissions = json_encode($this->permissions); | |
$this->user->permissions = $permissions; | |
\User::where_account_user_id($this->user->account_user_id) | |
->update([ | |
'permissions' => $permissions | |
]); | |
} | |
} |
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 | |
class PermissionsTest extends PHPUnit_Framework_TestCase | |
{ | |
public $user; | |
public function setUp() | |
{ | |
$this->user = User::create([ | |
'permissions' => '' | |
]); | |
$this->user->account_user_id = $this->user->id; | |
$this->user->save(); | |
} | |
public function tearDown() | |
{ | |
$this->user->delete(); | |
} | |
public function testAddingTrueBoolPermissions() | |
{ | |
$p = (new \Modules\User\Permissions($this->user)) | |
->set([ | |
'rmc.access' | |
], true); | |
$this->assertTrue($p->has('rmc.access')); | |
$checkUser = User::find($this->user->id); | |
$this->assertEquals(1, strpos($checkUser->permissions, '"rmc.access":true')); | |
} | |
public function testAddingFalseBoolPermissions() | |
{ | |
$p = (new \Modules\User\Permissions($this->user)) | |
->set([ | |
'rmc.access' | |
], false); | |
$this->assertFalse($p->has('rmc.access')); | |
$checkUser = User::find($this->user->id); | |
$this->assertEquals(1, strpos($checkUser->permissions, '"rmc.access":false')); | |
} | |
public function testAddingNumericPermissions() | |
{ | |
$p = (new \Modules\User\Permissions($this->user)) | |
->set([ | |
'rmc.contact.limit' | |
], 100); | |
$this->assertEquals(100, $p->get('rmc.contact.limit')); | |
$checkUser = User::find($this->user->id); | |
$this->assertEquals(1, strpos($checkUser->permissions, '"rmc.contact.limit":100')); | |
} | |
public function testAddingStringPermissions() | |
{ | |
$p = (new \Modules\User\Permissions($this->user)) | |
->set([ | |
'rmc.contact.string' | |
], "Testing"); | |
$this->assertEquals("Testing", $p->get('rmc.contact.string')); | |
$checkUser = User::find($this->user->id); | |
$this->assertEquals(1, strpos($checkUser->permissions, '"rmc.contact.string":"Testing"')); | |
} | |
public function testHasUnsetPermission() | |
{ | |
$p = (new \Modules\User\Permissions($this->user)) | |
->set([ | |
'rmc.access' | |
], true); | |
$this->assertFalse($p->has('lmc.access')); | |
} | |
public function testGetUnsetPermission() | |
{ | |
$p = (new \Modules\User\Permissions($this->user)) | |
->set([ | |
'rmc.text_limit' | |
], true); | |
$this->assertFalse($p->get('lmc.text_limit')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment