Common models
This commit is contained in:
12
src/common/Action.php
Normal file
12
src/common/Action.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $id
|
||||||
|
* @property string $description
|
||||||
|
*/
|
||||||
|
class Action extends Model
|
||||||
|
{
|
||||||
|
}
|
63
src/common/Auth.php
Normal file
63
src/common/Auth.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Aldarien
|
||||||
|
* @property int $id
|
||||||
|
* @property int $user_id foreign=users.id
|
||||||
|
* @property DateTime $time
|
||||||
|
* @property string $selector
|
||||||
|
* @property string $token
|
||||||
|
* @property int $status length=1 default=1
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Auth extends Model
|
||||||
|
{
|
||||||
|
public static $_table = 'logins';
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
23
src/common/Location.php
Normal file
23
src/common/Location.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Aldarien
|
||||||
|
* @property int $id
|
||||||
|
* @property string $controller length=50
|
||||||
|
* @property string $action length=100
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Location extends Model
|
||||||
|
{
|
||||||
|
public static $_table = 'locations';
|
||||||
|
|
||||||
|
public function permissions()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Permission::class, 'location')->findMany();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
49
src/common/Permission.php
Normal file
49
src/common/Permission.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Aldarien
|
||||||
|
* @property int $id
|
||||||
|
* @property int $type length=1
|
||||||
|
* @property int $ext_id
|
||||||
|
* @property Action $action_id
|
||||||
|
* @property int $status
|
||||||
|
* ---NULL---
|
||||||
|
* @property int $location null foreign=locations.id
|
||||||
|
* @property int $all length=1 default=0
|
||||||
|
* @property int $access length=1 default=0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Permission extends Model
|
||||||
|
{
|
||||||
|
public static $_table = 'permissions';
|
||||||
|
|
||||||
|
/*protected $locations = null;
|
||||||
|
public function location()
|
||||||
|
{
|
||||||
|
if ($this->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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
64
src/common/Registry.php
Normal file
64
src/common/Registry.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $id
|
||||||
|
* @property User $user
|
||||||
|
* @property string $action
|
||||||
|
* @property string $time
|
||||||
|
*/
|
||||||
|
class Registry extends Model
|
||||||
|
{
|
||||||
|
public static $_table = 'registries';
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->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();
|
||||||
|
}
|
||||||
|
}
|
16
src/common/RegistryData.php
Normal file
16
src/common/RegistryData.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $id
|
||||||
|
* @property Registry $registry
|
||||||
|
* @property string $column
|
||||||
|
* @property mixed $old
|
||||||
|
* @property mixed $new
|
||||||
|
*/
|
||||||
|
class RegistryData extends Model
|
||||||
|
{
|
||||||
|
public static $_table = 'registry_data';
|
||||||
|
}
|
164
src/common/Role.php
Normal file
164
src/common/Role.php
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
use Incoviba\Common\Service\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Aldarien
|
||||||
|
* @property int $id
|
||||||
|
* @property string $description length=50
|
||||||
|
* @property int $level
|
||||||
|
* @property Role $inherits
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Role extends Model
|
||||||
|
{
|
||||||
|
public static $_table = 'roles';
|
||||||
|
|
||||||
|
public function inherits()
|
||||||
|
{
|
||||||
|
if ($this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
104
src/common/User.php
Normal file
104
src/common/User.php
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
use Incoviba\Common\Service\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User with Privileges
|
||||||
|
* @property int $id
|
||||||
|
* @property string $name length=50
|
||||||
|
* @property string $password
|
||||||
|
*/
|
||||||
|
class User extends Model
|
||||||
|
{
|
||||||
|
public static $_table = 'users';
|
||||||
|
|
||||||
|
public function password($password = null)
|
||||||
|
{
|
||||||
|
if ($password == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
26
src/common/UserRole.php
Normal file
26
src/common/UserRole.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\common;
|
||||||
|
|
||||||
|
use Incoviba\Common\Alias\OldModel as Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Aldarien
|
||||||
|
* @property int $user foreign=users.id
|
||||||
|
* @property int $role foreign=roles.id
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class UserRole extends Model
|
||||||
|
{
|
||||||
|
public static $_table = 'user_roles';
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'user')->findOne();
|
||||||
|
}
|
||||||
|
public function role()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Role::class, 'role')->findOne();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
Reference in New Issue
Block a user