Files
wevads-platform/app/controllers/OapiUsers.php
2026-02-26 03:06:17 +01:00

765 lines
23 KiB
PHP
Executable File

<?php declare(strict_types=1); namespace IR\App\Controllers; if (!defined('IR_START')) exit('<pre>No direct script access allowed</pre>');
/**
* @framework iResponse Framework
* @version 1.0
* @author Amine Idrissi <contact@iresponse.tech>
* @date 2019
* @name OapiUsers.php
*/
# core
use IR\Core\Application as Application;
# mvc
use IR\Mvc\Controller as Controller;
# models
use IR\App\Models\Admin\OapiUser as OapiUser;
use IR\App\Models\Admin\OapiUsersBounce as OapiUsersBounce;
# http
use IR\Http\Request as Request;
# helpers
use IR\App\Helpers\Authentication as Authentication;
use IR\App\Helpers\Page as Page;
use IR\App\Helpers\DataTable as DataTable;
use IR\App\Helpers\Permissions as Permissions;
# exceptions
use IR\Exceptions\Types\PageException as PageException;
/**
* @name OapiUsers
* @description OapiUsers Controller
*/
class OapiUsers extends Controller
{
/**
* @app
* @readwrite
*/
protected $app;
/**
* @app
* @readwrite
*/
protected $authenticatedUser;
/**
* @name init
* @description initializing process before the action method executed
* @once
* @protected
*/
public function init()
{
# set the current application to a local variable
$this->app = Application::getCurrent();
# connect to the database
$this->app->database('system')->connect();
# check for authentication
if(!Authentication::isUserAuthenticated())
{
Page::redirect($this->app->http->request->getBaseURL() . RDS . 'auth' . RDS . 'login.' . DEFAULT_EXTENSION);
}
# check users roles
Authentication::checkUserRoles();
# get the authenticated user
$this->authenticatedUser = Authentication::getAuthenticatedUser();
}
/**
* @name main
* @description the main action
* @before init
* @after closeConnections,checkForMessage
*/
public function main()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,__FUNCTION__);
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# preparing the columns array to create the list
$columnsArray = [
'id',
'email',
'message',
'status',
'created_date'
];
# creating the html part of the list
$columns = Page::createTableHeader($columnsArray);
$filters = Page::createTableFilters($columnsArray);
# set menu status
$this->masterView->set([
'oapi_management' => 'true',
//'oapi_servers' => 'true',
'oapi_users_show' => 'true'
]);
# set data to the page view
$this->pageView->set([
'columns' => $columns,
'filters' => $filters
]);
}
/**
* @name getAdmin
* @description the getAdmin action
* @before init
* @after closeConnections
*/
public function get()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'main');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# get post data
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
//print_r($data);exit;
if(count($data))
{
$url = $this->app->http->request->getBaseURL();
# preparing the columns array to create the list
$columns = [
'id',
'email',
'message',
'status',
'created_date'
];
//echo "<pre>";
//print_r(DataTable::init($data,'admin.oapi_users s',$columns,new OapiUser(),'oapi-users','DESC',null));exit;
# fetching the results to create the ajax list
die(json_encode(DataTable::init2($data,'admin.oapi_users s',$columns,new OapiUser(),'oapi-users','DESC',null)));
}
}
/**
* @name add
* @description the add action
* @before init
* @after closeConnections,checkForMessage
*/
public function add()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,__FUNCTION__);
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# set menu status
$this->masterView->set([
'oapi_management' => 'true',
'oapi_servers' => 'true',
'oapi_users_add' => 'true'
]);
}
/**
* @name save
* @description the save action
* @before init
* @after closeConnections
*/
public function save()
{
# get post data
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
$message = 'Internal server error !';
$flag = 'error';
if(count($data))
{
$update = false;
$OapiUser = new OapiUser();
$username = $this->authenticatedUser->getEmail();
# update case
if($this->app->utils->arrays->get($data,'id') > 0)
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'edit');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$update = true;
$message = 'Record updated succesfully !';
$OapiUser->setId(intval($this->app->utils->arrays->get($data,'id')));
$OapiUser->load();
$OapiUser->setLastUpdatedBy($username);
$OapiUser->setLastUpdatedDate(date('Y-m-d'));
}
else
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'add');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$message = 'Record stored succesfully !';
$OapiUser->setCreatedBy($username);
$OapiUser->setCreatedDate(date('Y-m-d'));
$OapiUser->setLastUpdatedBy($username);
$OapiUser->setLastUpdatedDate(date('Y-m-d'));
$OapiUser->setMessage('check');
}
$OapiUser->setStatus($this->app->utils->arrays->get($data,'oapi-status','Activated'));
$OapiUser->setEmail(trim($this->app->utils->arrays->get($data,'oapi-email')));
$OapiUser->setDomain(explode("@", strtolower(trim($this->app->utils->arrays->get($data,'oapi-email'))))[1]);
$OapiUser->setPassword(trim($this->app->utils->arrays->get($data,'oapi-password')));
$OapiUser->setTenantId(trim($this->app->utils->arrays->get($data,'tenant-id')));
$OapiUser->setClientId(trim($this->app->utils->arrays->get($data,'client-id')));
$OapiUser->setSecretId(trim($this->app->utils->arrays->get($data,'secret-id')));
$OapiUser->setToken(trim($this->app->utils->arrays->get($data,'token','')));
$result = $update == false ? $OapiUser->insert() : $OapiUser->update();
if($result > -1)
{
$flag = 'success';
}
}
# stores the message in the session
Page::registerMessage($flag, $message);
# redirect to lists page
Page::redirect();
}
/**
* @name edit
* @description the edit action
* @before init
* @after closeConnections,checkForMessage
*/
public function edit()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,__FUNCTION__);
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$arguments = func_get_args();
$id = isset($arguments) && count($arguments) > 0 ? $arguments[0] : null;
$valid = true;
# set menu status
$this->masterView->set([
'oapi_management' => 'true',
'oapi_servers' => 'true',
'oapi_servers_show' => 'true'
]);
if(!isset($id) || !is_numeric($id) || intval($id) == 0)
{
$valid = false;
}
$Account = OapiUser::first(OapiUser::FETCH_ARRAY,['id = ?',$id]);
if(count($Account) == 0)
{
$valid = false;
}
if($valid == true)
{
# set data to the page view
$this->pageView->set([
'Account' => $Account
]);
}
else
{
# stores the message in the session
Page::registerMessage('error','Invalid OApi Account id !');
# redirect to lists page
Page::redirect();
}
}
/**
* @name users
* @description the users action
* @before init
* @after closeConnections
*/
public function users()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'edit');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# set menu status
$this->masterView->set([
'oapi_management' => 'true',
'oapi_servers' => 'true',
'oapi_servers_show' => 'true'
]);
$arguments = func_get_args();
$page = isset($arguments) && count($arguments) ? $arguments[0] : '';
if(isset($page) && $page != '')
{
switch ($page)
{
case 'get' :
{
# get post data
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
if(count($data))
{
$accountId = isset($arguments) && count($arguments) ? intval($arguments[1]) : 0;
# preparing the columns array to create the list
$columns = [
'id',
'email',
'message',
'status',
'created_date'
];
# fetching the results to create the ajax list
$query = $this->app->database('system')->query()->from('admin.oapi_users',$columns)->where('admin_id = ?',$accountId);
die(json_encode(DataTable::init($data,'admin.oapi_users s',$columns,new OapiUser(),'oapi-users','DESC',$query)));
}
break;
}
}
}
}
/**
* @name multiAdd
* @description the multiAdd action
* @before init
* @after closeConnections,checkForMessage
*/
public function multiAdd()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'add');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
$message = 'Internal server error !';
$flag = 'error';
if(count($data)){
$username = $this->authenticatedUser->getEmail();
$users = array_filter(array_unique(explode(PHP_EOL,$this->app->utils->arrays->get($data,'users'))));
//exit;
if(!is_array($users) || count($users) == 0)
{
$message = 'Users not found !';
}
else
{
$result = -1;
foreach ($users as $user)
{
$userInfos = explode(";",str_replace(["\n","\r"], "", $user));
if(count(array_filter(array_unique($userInfos)))<5){
continue;
}
//$res = OapiUser::first(OapiUser::FETCH_ARRAY,["email = ?",$userInfos[0]],['id'],'id','DESC');
//if(count($res) == 0)
//{
$oapiUser = new OapiUser();
$oapiUser->setCreatedBy($username);
$oapiUser->setCreatedDate(date('Y-m-d'));
$oapiUser->setLastUpdatedBy($username);
$oapiUser->setLastUpdatedDate(date('Y-m-d'));
$oapiUser->setStatus('Activated');
$oapiUser->setMessage('check');
$oapiUser->setEmail(trim($userInfos[0]));
$oapiUser->setDomain(explode("@", strtolower(trim($userInfos[0])))[1]);
$oapiUser->setPassword(trim($userInfos[1]));
$oapiUser->setClientId(trim($userInfos[2]));
$oapiUser->setTenantId(trim($userInfos[3]));
$oapiUser->setSecretId(trim($userInfos[4]));
$result += $oapiUser->insert();
$index++;
//}
}
if($result > -1)
{
$message = 'Records Add succesfully !';
$flag = 'success';
}
}
# stores the message in the session
Page::registerMessage($flag, $message);
# redirect to lists page
Page::redirect();
}
# set menu status
$this->masterView->set([
'oapi_management' => 'true',
'oapi_users_multi_add' => 'true'
]);
}
/**
* @name multiDelete
* @description the multiDelete action
* @before init
* @after closeConnections,checkForMessage
*/
public function multiDelete()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'delete');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
$message = 'Internal server error !';
$flag = 'error';
if(count($data)){
$username = $this->authenticatedUser->getEmail();
$users = array_filter(array_unique(explode(PHP_EOL,$this->app->utils->arrays->get($data,'users'))));
//exit;
if(!is_array($users) || count($users) == 0)
{
$message = 'Users not found !';
}
else
{
$result = -1;
foreach ($users as $user)
{
$user=str_replace(["\n","\r"," ",'"',","], "",$user);
if (strpos($user, "@") !== false) {
//res = OapiUser::first(OapiUser::FETCH_ARRAY,["email = ?",$user],['id'],'id','DESC');
$result +=OapiUser::deleteWhere("email = ?",[$user]);
/*
if(count($res) > 0)
{
$OapiUser = new OapiUser();
$OapiUser->setId(intval($res['id']));
$OapiUser->load();
//print_r($OapiUser);exit;
$result += $OapiUser->delete();
}
*/
}else{
$result +=OapiUser::deleteWhere('email like ?',["%@".$user]);
}
}
if($result > -1)
{
$message = 'Records delete succesfully !';
$flag = 'success';
}
}
# stores the message in the session
Page::registerMessage($flag, $message);
# redirect to lists page
Page::redirect();
}
# set menu status
$this->masterView->set([
'oapi_management' => 'true',
'oapi_users_multi_delete' => 'true'
]);
}
/**
* @name multiGet
* @description the multiDelete action
* @before init
* @after closeConnections,checkForMessage
*/
public function multiGet()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'main');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
$message = 'Internal server error !';
$flag = 'error';
if(count($data)){
$username = $this->authenticatedUser->getEmail();
$users = array_filter(array_unique(explode(PHP_EOL,$this->app->utils->arrays->get($data,'users'))));
//exit;
if(!is_array($users) || count($users) == 0)
{
$message = 'Users not found !';
}
else
{
$result = -1;
$user=[];
foreach ($users as $use)
{
$user[]=str_replace(["\n","\r"," ","'",'"'], "",$use);
}
$allUsers=[];
if(count($user) > 0)
{
$allUsers = OapiUser::all(OapiUser::FETCH_ARRAY,['email IN ?',[$user]],["email","password","client_id","tenant_id"]);
}
if(count($allUsers) > 0)
{
$message = 'users show succesfully !';
$flag = 'success';
# set data to the page view
$this->pageView->set([
'users' => $allUsers,
'ids' => $user
]);
}
}
# stores the message in the session
Page::registerMessage($flag, $message);
# redirect to lists page
//Page::redirect();
}
# set menu status
$this->masterView->set([
'oapi_management' => 'true',
'oapi_users_multi_get' => 'true'
]);
}
/**
* @name main
* @description the main action
* @before init
* @after closeConnections,checkForMessage
*/
public function bounce()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'delete');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# preparing the columns array to create the list
$columnsArray = [
'id',
'user_id',
'user_email',
'bounce',
'message',
'created_date'
];
$activeUsers = $this->app->database('system')->query()->from('admin.oapi_users')->where('message = ?','ok')->count();
$bounce = $this->app->database('system')->execute("select sum(bounce) from admin.oapi_users_bounce ");
# creating the html part of the list
$columns = Page::createTableHeader($columnsArray);
$filters = Page::createTableFilters($columnsArray);
# set menu status
$this->masterView->set([
'oapi_management' => 'true',
//'oapi_servers' => 'true',
'oapi_users_bounce' => 'true'
]);
# set data to the page view
$this->pageView->set([
'columns' => $columns,
'filters' => $filters,
'users' => $activeUsers,
'bounce' => $bounce[0]["sum"]
]);
}
/**
* @name getAdmin
* @description the getAdmin action
* @before init
* @after closeConnections
*/
public function getUsersBounce()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'delete');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# get post data
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
//print_r($data);exit;
if(count($data))
{
$url = $this->app->http->request->getBaseURL();
# preparing the columns array to create the list
$columns = [
'id',
'user_id',
'user_email',
'bounce',
'message',
'created_date'
];
# fetching the results to create the ajax list
die(json_encode(DataTable::init2($data,'admin.oapi_users_bounce s',$columns,new OapiUsersBounce(),'oapi-users','DESC',null,false)));
}
}
/**
* @name closeConnections
* @description close all connections
* @once
* @protected
*/
public function closeConnections()
{
# connect to the database
$this->app->database('system')->disconnect();
$this->app->database('clients')->disconnect();
}
/**
* @name checkForMessage
* @description checks for session messages
* @once
* @protected
*/
public function checkForMessage()
{
# check for message
Page::checkForMessage($this);
}
}