Changes to the auth method. !!Incomplete!!

This commit is contained in:
ahwelp
2023-01-24 20:40:01 +00:00
parent 78bd7cc06d
commit 357baca274
20 changed files with 877 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
<?php
namespace App\Core\Auth\Classes;
class Auth{
}
@@ -0,0 +1,42 @@
<?php
namespace App\Core\Auth\Classes;
use Singleton;
/**
* Store the user auth and permissions on a singleton
* so this will be used on ws and web requests
*/
class AuthenticatedUser extends Singleton
{
private $user;
private $permissions;
public static function load(User $user = null)
{
$instance = self::getInstance();
if ($user) {
$instance->user = $user;
$instance->permissions = $user->permissions();
}
if ($_SESSION['user']) {
$instance->user = $_SESSION['user'];
$instance->permissions = $_SESSION['user']['permission'];
}
return $instance;
}
public static function user()
{
return self::getInstance()->user;
}
public static function permissions()
{
return self::getInstance()->permissions;
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Core\Auth\Classes;
use ORM\Entity as Entity;
use App\Core\Auth\Classes\Permission as Permission;
class Group extends Entity{
const _tableName = "auth_group";
function permissions(){
return $this->belongsToMany(Permission::class, 'auth_permission_group', 'group_id', 'permission_id');
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Core\Auth\Classes;
use ORM\Entity as Entity;
class Permission extends Entity{
const _tableName = "auth_permission";
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Core\Auth\Classes;
use Singleton;
/**
* Store the user auth and permissions on a singleton
* so this will be used on ws and web requests
*/
class SessionLog extends Singleton
{
private static $session;
private static $logs = Array();
/**
* Opens a session object.
* The session will represent a request initiation
*/
public static function openSession()
{
}
/**
* Add a log to the session
* A log will store a hasPermission request
*/
public static function storeLog(Permission $permission)
{
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Core\Auth\Classes;
use ORM\Entity as Entity;
use App\Core\Auth\Classes\User as User;
class Token extends Entity{
const _tableName = "auth_token";
function user(){
return $this->belongsTo(User::class, 'user_id')->get();
}
}
+74
View File
@@ -0,0 +1,74 @@
<?php
namespace App\Core\Auth\Classes;
use ORM\Entity as Entity;
use App\Core\Auth\Classes\Group as Group;
use App\Core\Auth\Classes\Permission as Permission;
use ORM\DBInstance;
class User extends Entity{
public $method = 'manual';
public $username = '';
public $name = '';
public $email = '';
public $password = '';
const _tableName = "auth_user";
const _timestamps = true;
const _softdelete = true;
function groups(){
return $this->belongsToMany(Group::class, 'auth_group_user', 'user_id', 'group_id')->get();
}
function specificPermissions(){
return $this->belongsToManyExtended(Permission::class, 'auth_permission_user', 'user_id', 'permission_id');
}
function permissions(){
$permissions = [];
foreach ($this->groups() as $group){
foreach ($group->permissions()->get() as $permission){
$permissions[] = $permission->name;
}
}
foreach ($this->specificPermissions()->get() as $permission){
if($permission->action == 'a'){
$permissions[] = $permission->childElement->name;
}else{
}
}
return $permissions;
}
function groupsoptions(){
$groupList = Group::findAll()->get();
foreach($this->groups() as $group){
foreach($groupList as $key => $list){
if($group->id == $list->id){
$groupList[$key]->selected = true;
}
}
}
return $groupList;
}
public function appendGroups($groups){
DBInstance::execute('DELETE FROM {auth_group_user} WHERE user_id = ?', [$this->id]);
foreach($groups as $group){
DBInstance::insertRecord('auth_group_user', ['user_id' => $this->id, 'group_id' => $group]);
}
}
}