From 50c2bb0f156586af028d61fe3c492c6b05a39ccc Mon Sep 17 00:00:00 2001 From: Aldarien Date: Mon, 23 Dec 2019 18:01:06 -0300 Subject: [PATCH] Common models --- src/common/Action.php | 12 +++ src/common/Auth.php | 63 ++++++++++++++ src/common/Location.php | 23 +++++ src/common/Permission.php | 49 +++++++++++ src/common/Registry.php | 64 ++++++++++++++ src/common/RegistryData.php | 16 ++++ src/common/Role.php | 164 ++++++++++++++++++++++++++++++++++++ src/common/User.php | 104 +++++++++++++++++++++++ src/common/UserRole.php | 26 ++++++ 9 files changed, 521 insertions(+) create mode 100644 src/common/Action.php create mode 100644 src/common/Auth.php create mode 100644 src/common/Location.php create mode 100644 src/common/Permission.php create mode 100644 src/common/Registry.php create mode 100644 src/common/RegistryData.php create mode 100644 src/common/Role.php create mode 100644 src/common/User.php create mode 100644 src/common/UserRole.php diff --git a/src/common/Action.php b/src/common/Action.php new file mode 100644 index 0000000..8a46229 --- /dev/null +++ b/src/common/Action.php @@ -0,0 +1,12 @@ +belongsTo(User::class, 'user_id')->findOne(); + } + public function token($token = null) + { + if ($token == null) { + return false; + } + $this->token = \password_hash($token, \PASSWORD_DEFAULT); + } + public function time($time = null) + { + if ($time == null) { + return Carbon::parse($this->time, config('app.timezone')); + } + if (!\is_a($time, \DateTime::class)) { + $time = Carbon::parse($time, config('app.timezone')); + } + $this->time = $time; + } + public function save() + { + if (!\is_string($this->time)) { + $this->time = $this->time->format('Y-m-d H:i:s'); + } + parent::save(); + } + public function isIn() + { + if ($this->status == 0) { + return false; + } + $now = Carbon::now(config('app.timezone')); + $diff = $now->diffAsCarbonInterval($this->time, true); + if ($diff->totalHours > config('app.login_hours')) { + return false; + } + return true; + } +} +?> diff --git a/src/common/Location.php b/src/common/Location.php new file mode 100644 index 0000000..5d3323d --- /dev/null +++ b/src/common/Location.php @@ -0,0 +1,23 @@ +hasMany(Permission::class, 'location')->findMany(); + } +} +?> diff --git a/src/common/Permission.php b/src/common/Permission.php new file mode 100644 index 0000000..c283e7e --- /dev/null +++ b/src/common/Permission.php @@ -0,0 +1,49 @@ +all == 0) { + return $this->belongsTo(Location::class, 'location')->findOne(); + } + if ($this->locations == null) { + $this->locations = \Model::factory(Location::class)->findMany(); + } + return $this->locations; + }*/ + public function who() + { + switch ($this->type) { + case 1: + return $this->belongsTo(User::class, 'ext_id')->findOne(); + case 2: + return $this->belongsTo(Role::class, 'ext_id')->findOne(); + } + } + public function action() + { + return $this->belongsTo(Action::class, 'action_id')->findOne(); + } +} +?> diff --git a/src/common/Registry.php b/src/common/Registry.php new file mode 100644 index 0000000..aae8a0a --- /dev/null +++ b/src/common/Registry.php @@ -0,0 +1,64 @@ +belongsTo(User::class, 'user')->findOne(); + } + protected $model; + public function model() + { + if ($this->model == null) { + list($model, $actions) = explode(']', $this->action); + $model = str_replace(['Incoviba\\old\\', 'Incoviba\\nuevo\\', '\\'], ['', '', '->'], trim($model, '[')); + $this->model = $model; + } + return $this->model; + } + protected $actions; + public function actions() + { + if ($this->actions == null) { + list($model, $actions) = explode(']', $this->action); + $actions = explode(', ', trim($actions)); + $resultados = []; + foreach ($actions as $action) { + if (strpos($action, ': ') !== false) { + list($columna, $valor) = explode(': ', $action); + } else { + $columna = ''; + $valor = $action; + } + if (strpos($valor, ' -> ') !== false) { + list($old, $new) = explode(' -> ', $valor); + } else { + $old = ''; + $new = $valor; + } + $resultados[$columna] = (object) ['old' => $old, 'new' => $new]; + } + $this->actions = $resultados; + } + return $this->actions; + } + public function time(Carbon $time = null) + { + if ($time == null) { + return Carbon::parse($this->time); + } + $this->time = $time->toDateTimeString(); + } +} diff --git a/src/common/RegistryData.php b/src/common/RegistryData.php new file mode 100644 index 0000000..18dee39 --- /dev/null +++ b/src/common/RegistryData.php @@ -0,0 +1,16 @@ +inherits != 0) { + return $this->belongsTo(Role::class, 'inherits')->findOne(); + } + return false; + } + protected $permissions; + public function permissions() + { + if ($this->permissions == null) { + $permissions = $this->hasMany(Permission::class, 'ext_id')->where('permissions.type', 2)->findMany(); + if ($this->inherits()) { + $permissions = array_merge($permissions, $this->inherits()->permissions()); + } + usort($permissions, function($a, $b) { + return strcmp($a->action()->description, $b->action()->description); + }); + $this->permissions = $permissions; + } + return $this->permissions; + } + public function users() + { + return $this->hasManyThrough(User::class, UserRole::class, 'role', 'user')->findMany(); + } + public function hasAccess($route) + { + $action = $route->getArgument('action'); + $action = (new Factory(Action::class))->where(['description' => $action])->find(); + if (!$action) { + return false; + } + + $data = [ + 'type' => 2, + 'ext_id' => $this->id, + 'action_id' => $action->id, + 'status' => 1 + ]; + $permission = (new Factory(Permission::class))->where($data)->find(); + if ($permission !== false) { + return true; + } + + if ($this->inherits()) { + return $this->inherits()->hasAccess($route); + } + return false; + } + public function checkAccess($action_name) + { + $action = (new Factory(Action::class))->where(['description' => $action_name])->find(); + if (!$action) { + throw new \Exception('Action ' . $action_name . ' not found.'); + } + $permission = (new Factory(Permission::class))->where([ + 'type' => 2, + 'ext_id' => $this->id, + 'action_id' => $action->id, + 'status' => 1 + ])->find(); + if ($permission !== false) { + return true; + } + if ($this->inherits()) { + return $this->inherits()->checkAccess($action_name); + } + return false; + } + public function addPermission($action_name) + { + if ($this->checkAccess($action_name)) { + return; + } + $action = (new Factory(Action::class))->where(['description' => $action_name])->find(); + if (!$action) { + throw new \InvalidArgumentException($action_name . ' not found.'); + } + $data = [ + 'type' => 2, + 'ext_id' => $this->id, + 'action_id' => $action->id + ]; + $permission = (new Factory(Permission::class))->where($data)->find(); + if (!$permission) { + $permission = (new Factory(Permission::class))->create($data); + } + $permission->status = 1; + $permission->save(); + } + public function removePermission($action_name) + { + if (!$this->checkAccess($action_name)) { + return; + } + $action = (new Factory(Action::class))->where(['description' => $action_name])->find(); + if (!$action) { + throw new \InvalidArgumentException($action_name . ' not found.'); + } + $data = [ + 'type' => 2, + 'ext_id' => $this->id, + 'action_id' => $action->id + ]; + $permission = (new Factory(Permission::class))->where($data)->find(); + if (!$permission) { + return; + } + $permission->status = 0; + $permission->save(); + } + public function hasUser($user) + { + $user = \Model::factory(User::class) + ->select('users.*') + ->join('user_roles', ['user_roles.user', '=', 'users.id']) + ->join('roles', ['roles.id', '=', 'user_roles.role']) + ->where('roles.id', $this->id) + ->whereAnyIs([['users.name' => $user], ['users.id' => $user]]) + ->findOne(); + if ($user !== false) { + return true; + } + return false; + } + public function isInherited($action_name) + { + if (!$this->checkAccess($action_name)) { + return false; + } + $action = (new Factory(Action::class))->where(['description' => $action_name])->find(); + $permission = (new Factory(Permission::class))->where([ + 'type' => 2, + 'ext_id' => $this->id, + 'action_id' => $action->id, + 'status' => 1 + ])->find(); + if ($permission !== false) { + return false; + } + return true; + } +} +?> diff --git a/src/common/User.php b/src/common/User.php new file mode 100644 index 0000000..57d1aa9 --- /dev/null +++ b/src/common/User.php @@ -0,0 +1,104 @@ +password = \password_hash($password, \PASSWORD_BCRYPT); + } + protected $permissions = null; + public function permissions() + { + if ($this->permissions == null) { + $permissions = $this->hasMany(Permission::class, 'ext_id')->where('permissions.type', 1)->findMany(); + if ($permissions == false) { + $permissions = []; + } + foreach ($this->roles() as $role) { + $rp = $role->permissions(); + if ($rp !== false) { + $permissions = array_merge($permissions, $rp); + } + } + if (count($permissions) == 0) { + $permissions = false; + } + $this->permissions = $permissions; + } + return $this->permissions; + } + public function roles() + { + return $this->hasManyThrough(Role::class, UserRole::class, 'user', 'role')->findMany(); + } + public function hasAccess($route) + { + foreach ($this->roles() as $role) { + if ($role->hasAccess($route) === true) { + return true; + } + } + + $action = $route->getArgument('action'); + $action = (new Factory(Action::class))->where(['description' => $action])->find(); + if (!$action) { + return false; + } + + $data = [ + 'type' => 1, + 'ext_id' => $this->id, + 'action_id' => $action->id, + 'status' => 1 + ]; + $permission = (new Factory(Permission::class))->where($data)->find(); + if ($permission !== false) { + return true; + } + return false; + } + public function checkAccess($action_name) + { + foreach ($this->roles() as $role) { + if ($role->checkAccess($action_name) === true) { + return true; + } + } + $action = (new Factory(Action::class))->where(['description' => $action_name])->find(); + + $permission = (new Factory(Permission::class))->where([ + 'type' => 1, + 'ext_id' => $this->id, + 'action_id' => $action->id, + 'status' => 1 + ])->find(); + if ($permission !== false) { + return true; + } + return false; + } + public function hasRole($role) + { + foreach ($this->roles() as $r) { + if ($r->description == $role or $r->id == $role) { + return true; + } + } + return false; + } +} diff --git a/src/common/UserRole.php b/src/common/UserRole.php new file mode 100644 index 0000000..646bf01 --- /dev/null +++ b/src/common/UserRole.php @@ -0,0 +1,26 @@ +belongsTo(User::class, 'user')->findOne(); + } + public function role() + { + return $this->belongsTo(Role::class, 'role')->findOne(); + } +} +?>