Skip to content

Commit

Permalink
=
Browse files Browse the repository at this point in the history
  • Loading branch information
arikaim-repository committed Feb 28, 2020
1 parent be65988 commit e79a5ac
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 25 deletions.
2 changes: 1 addition & 1 deletion App/ArikaimStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ArikaimStore
/**
* Arikaim store host
*/
const HOST = 'http://arikaim.com';
const HOST = 'http://arikaim.com';
const SIGNUP_URL = Self::HOST . '/signup';

/**
Expand Down
12 changes: 7 additions & 5 deletions App/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ public function install()

// add control panel permisison item
$result = Arikaim::access()->addPermission(Access::CONTROL_PANEL,Access::CONTROL_PANEL,'Arikaim control panel access.');
if ($result == false) {
$this->addError("Can't create contorl panel permission");
if ($result == false) {
if (Model::Permissions()->has(Access::CONTROL_PANEL) == false) {
$this->addError("Can't create contorl panel permission");
}
}

// register core events
Expand Down Expand Up @@ -193,7 +195,7 @@ private function createDefaultAdminUser()
return false;
}
}

return Model::PermissionRelations()->setUserPermission(Access::CONTROL_PANEL,Access::FULL,$user->id);
}

Expand Down Expand Up @@ -265,13 +267,13 @@ private function createDbTables()

foreach ($classes as $class) {
$installed = Schema::install($class);

if ($installed == false) {
$errors++;
$error = 'Error create database table "' . Schema::getTable($class) . '"';
$this->addError($error);
}
}
}

return ($errors == 0);
}
Expand Down
17 changes: 17 additions & 0 deletions Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ public function installDriver($name, $class = null, $category = null, $title = n
return $result;
}

/**
* Uninstall driver
*
* @param string $name Driver name
* @return boolean
*/
public function unInstallDriver($name)
{
$result = Arikaim::driver()->unInstall($name);
if ($result !== true) {
// add error
$this->addError(Arikaim::errors()->getError("ERROR_UNINSTALL_DRIVER",['name' => $name]));
}

return $result;
}

/**
* Return extension name
*
Expand Down
37 changes: 28 additions & 9 deletions Models/PermissionRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,34 @@ public function getUserPermissions($userId)
return $query->get();
}

/**
* Get user group permisssions
*
* @param integer $groupId
* @return mixed
*/
public function getGroupPermissions($groupId)
{
$query = $this->getRelationsQuery($groupId,'group');

return $query->get();
}

/**
* Set user permission
*
* @param string $name
* @param array $permissions
* @param integer|string|null $id
* @return bool
* @return Model|bool
*/
public function setUserPermission($name, $permissions, $id = null)
{
if (is_string($id) == true) {
$model = DbModel::Users()->findById($id);
$id = (is_object($model) == true) ? $model->id : null;
}

return $this->setPermission($name,$permissions,$id,Self::USER);
}

Expand All @@ -124,14 +138,15 @@ public function setUserPermission($name, $permissions, $id = null)
* @param string $name
* @param array $permissions
* @param integer|string $id
* @return bool
* @return Model|bool
*/
public function setGroupPermission($name, $permissions, $id)
{
if (is_string($id) == true) {
$model = DbModel::UserGroups()->findById($id);
$id = (is_object($model) == true) ? $model->id : null;
}

return $this->setPermission($name,$permissions,$id,Self::GROUP);
}

Expand Down Expand Up @@ -220,21 +235,24 @@ public function getPermission($name, $id, $type = Self::USER)
* @param array $access - ['read','write','delete','execute]
* @param integer|null $id user Id or group Id
* @param integer $type
* @return bool
* @return Model|bool
*/
public function setPermission($name, $access, $id = null, $type = Self::USER)
{
$permissions = $this->resolvePermissions($access);
$id = ($id == null && $type == Self::USER) ? Arikaim::access()->getId() : $id;
$relationId = DbModel::Permissions()->getId($name);
if (empty($relationId) == true) {
$permissionId = DbModel::Permissions()->getId($name);
if (empty($permissionId) == true) {
return false;
}
$model = $this->saveRelation($id,$type,$relationId);

$result = (is_object($model) == true) ? $model->update($permissions) : false;
$model = $this->saveRelation($permissionId,$type,$id);

if (is_object($model) == false) {
$model = $this->getRelation($permissionId,$type,$id);
}
$result = $model->update($permissions);

return $result;
return ($result === false) ? false : $model;
}

/**
Expand Down Expand Up @@ -336,6 +354,7 @@ public function hasFull()
$count += ($this->hasPermission('write') == false) ? 0 : 1;
$count += ($this->hasPermission('delete') == false) ? 0 : 1;
$count += ($this->hasPermission('execute') == false) ? 0 : 1;

return ($count == 4) ? true : false;
}
}
28 changes: 27 additions & 1 deletion Models/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@

use Illuminate\Database\Eloquent\Model;

use Arikaim\Core\Access\Access;

use Arikaim\Core\Db\Traits\Uuid;
use Arikaim\Core\Db\Traits\Find;
use Arikaim\Core\Db\Traits\Slug;

/**
* Permissions database model
*/
class Permissions extends Model
{
use Uuid,
Slug,
Find;

/**
Expand All @@ -29,9 +33,12 @@ class Permissions extends Model
*/
protected $fillable = [
'name',
'slug',
'editable',
'title',
'description',
'extension_name'
'extension_name',
'validator_class'
];

/**
Expand All @@ -48,6 +55,13 @@ class Permissions extends Model
*/
protected $table = 'permissions';

/**
* Slug source column
*
* @var string
*/
protected $slugSourceColumn = 'name';

/**
* Mutator (get) for title attribute.
*
Expand All @@ -67,6 +81,7 @@ public function getTitleAttribute()
public function has($name)
{
$model = $this->where('name','=',$name)->first();

return (is_object($model) == false) ? false : true;
}

Expand All @@ -79,6 +94,17 @@ public function has($name)
public function getId($name)
{
$model = $this->where('name','=',$name)->first();

return (is_object($model) == true) ? $model->id : false;
}

/**
* Get permisisons list query
*
* @return Builder
*/
public function getListQuery()
{
return $this->where('name','<>',Access::CONTROL_PANEL)->orderBy('name');
}
}
16 changes: 14 additions & 2 deletions Models/Schema/PermissionsSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ public function create($table)
$table->id();
$table->prototype('uuid');
$table->string('name')->nullable(false);
$table->slug();
$table->integer('editable')->nullable(true);
$table->string('extension_name')->nullable(true);
$table->string('title')->nullable(true);
$table->string('description')->nullable(true);
$table->string('description')->nullable(true);
$table->string('validator_class')->nullable(true);
// indexes
$table->unique('name');
$table->index('extension_name');
Expand All @@ -50,6 +53,15 @@ public function create($table)
* @return void
*/
public function update($table)
{
{
if ($this->hasColumn('validator_class') == false) {
$table->string('validator_class')->nullable(true);
}
if ($this->hasColumn('slug') == false) {
$table->slug();
}
if ($this->hasColumn('editable') == false) {
$table->integer('editable')->nullable(true);
}
}
}
6 changes: 5 additions & 1 deletion Models/Schema/UserGroupMembersSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function create($table)
$table->id();
$table->userId();
$table->relation('group_id','user_groups',false);

$table->prototype('uuid');

// date time
$table->dateCreated();
$table->dateUpdated();
Expand All @@ -52,5 +53,8 @@ public function create($table)
*/
public function update($table)
{
if ($this->hasColumn('uuid') == false) {
$table->prototype('uuid');
}
}
}
10 changes: 9 additions & 1 deletion Models/Schema/UserGroupsSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function create($table)
// columns
$table->id();
$table->prototype('uuid');
$table->status();
$table->slug();
$table->string('title')->nullable(false);
$table->string('description')->nullable(true);
// unique indexes
Expand All @@ -46,7 +48,13 @@ public function create($table)
* @param \Arikaim\Core\Db\TableBlueprint $table
* @return void
*/
public function update($table)
public function update($table)
{
if ($this->hasColumn('status') == false) {
$table->status();
}
if ($this->hasColumn('slug') == false) {
$table->slug();
}
}
}
38 changes: 35 additions & 3 deletions Models/UserGroupMembers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Arikaim\Core\Models\UserGroups;

use Arikaim\Core\Db\Traits\Uuid;
use Arikaim\Core\Db\Traits\Find;
use Arikaim\Core\Db\Traits\DateCreated;

/**
Expand All @@ -23,6 +24,7 @@
class UserGroupMembers extends Model
{
use Uuid,
Find,
DateCreated;

/**
Expand All @@ -32,8 +34,9 @@ class UserGroupMembers extends Model
*/
protected $fillable = [
'user_id',
'groupd_id',
'date_expire'
'group_id',
'date_expire',
'date_created'
];

/**
Expand Down Expand Up @@ -67,6 +70,35 @@ public function group()
*/
public function user()
{
return $this->hasOne(Users::class,'user_id','id');
return $this->belongsTo(Users::class,'user_id');
}

/**
* Add member to group
*
* @param string|integer $userId
* @param string|integer $groupId
* @return Model|false
*/
public function addMember($userId, $groupId)
{
$user = new Users();
$user = $user->findById($userId);
if (is_object($user) == false) {
return false;
}

$group = new UserGroups();
$group = $group->findById($groupId);
if (is_object($group) == false) {
return false;
}

$member = $this->create([
'user_id' => $user->id,
'group_id' => $group->id
]);

return (is_object($member) == true) ? $member : false;
}
}
Loading

0 comments on commit e79a5ac

Please sign in to comment.