Files
fmgapp/app/controllers/FapiAccounts.php

479 lines
14 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 FapiAccounts.php
*/
# core
use IR\Core\Application as Application;
# mvc
use IR\Mvc\Controller as Controller;
# models
use IR\App\Models\Admin\FapiAdmin as FapiAdmin;
use IR\App\Models\Admin\FapiAccount as FapiAccount;
use IR\App\Models\Admin\Proxy as Proxy;
# 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 FapiAccounts
* @description FapiAccounts Controller
*/
class FapiAccounts 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',
'name',
'admin_name',
'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([
'fapi_management' => 'true',
'fapi_servers' => 'true',
'fapi_servers_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',
'name',
'admin_name',
'message',
'status',
'created_date'
];
# fetching the results to create the ajax list
die(json_encode(DataTable::init2($data,'admin.fapi_accounts s',$columns,new FapiAccount(),'fapi-accounts','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([
'fapi_management' => 'true',
'fapi_servers' => 'true',
'fapi_servers_add' => 'true'
]);
# set data to the page view
$this->pageView->set([
'FapiAdmin' => FapiAdmin::all(FapiAdmin::FETCH_ARRAY,['status = ?','Activated'],['id','name'],'id','ASC')
]);
}
/**
* @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;
$fapiAccount = new FapiAccount();
$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 !';
$fapiAccount->setId(intval($this->app->utils->arrays->get($data,'id')));
$fapiAccount->load();
$fapiAccount->setLastUpdatedBy($username);
$fapiAccount->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 !';
$fapiAccount->setCreatedBy($username);
$fapiAccount->setCreatedDate(date('Y-m-d'));
$fapiAccount->setLastUpdatedBy($username);
$fapiAccount->setLastUpdatedDate(date('Y-m-d'));
}
$fapiAdmin = FapiAdmin::first(FapiAdmin::FETCH_ARRAY,['id = ?',intval($this->app->utils->arrays->get($data,'admin-id'))]);
$result = -1;
if(count($fapiAdmin) == 0)
{
$message = 'Fapi Admin not found !';
}
else
{
$token = json_decode($this->app->utils->arrays->get($data,'fapi-token'),true);
$credentials = json_decode($this->app->utils->arrays->get($data,'fapi-credentials'),true);
if(isset($credentials['private_key_id']) && isset($token['apiKey']))
{
$fapiAccount->setAdminId(intval($this->app->utils->arrays->get($fapiAdmin,'id')));
$fapiAccount->setAdminName($this->app->utils->arrays->get($fapiAdmin,'name'));
$fapiAccount->setName(trim($this->app->utils->arrays->get($data,'fapi-name')));
$fapiAccount->setStatus($this->app->utils->arrays->get($data,'fapi-status','Activated'));
$fapiAccount->setToken(json_encode($token));
$fapiAccount->setCredential(json_encode($credentials));
$result = $update == false ? $fapiAccount->insert() : $fapiAccount->update();
if($result > -1)
{
$flag = 'success';
}
}else{
$message = 'Credentials or Token Incorrect Format !';
}
}
}
# 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([
'gapi_management' => 'true',
'gapi_servers' => 'true',
'gapi_servers_show' => 'true'
]);
if(!isset($id) || !is_numeric($id) || intval($id) == 0)
{
$valid = false;
}
$FapiAccount = FapiAccount::first(FapiAccount::FETCH_ARRAY,['id = ?',$id]);
if(count($FapiAccount) == 0)
{
$valid = false;
}
if($valid == true)
{
$credentials='';
$token='';
if($FapiAccount['credential']!=""){
$credentials=json_encode(json_decode($FapiAccount['credential'],true),JSON_PRETTY_PRINT);
$credentials=str_replace("\/", "/", $credentials);
}
if($FapiAccount['token']!=""){
$token=json_encode(json_decode($FapiAccount['token'],true),JSON_PRETTY_PRINT);
$token=str_replace("\/", "/", $token);
}
# set data to the page view
$this->pageView->set([
'fapiAccount' => $FapiAccount,
'credentials' => $credentials,
'token' => $token,
'fapiAdmin' => FapiAdmin::all(FapiAdmin::FETCH_ARRAY,['status = ?','Activated'],['id','name'],'id','ASC')
]);
}
else
{
# stores the message in the session
Page::registerMessage('error','Invalid gapi user id !');
# redirect to lists page
Page::redirect();
}
}
/**
* @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) {
$result +=FapiAccount::deleteWhere("LOWER(email) = ?",[ strtolower($user)]);
}else{
$result +=FapiAccount::deleteWhere('LOWER(email) like ?',["%@".strtolower($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([
'gapi_management' => 'true',
'gapi_users_multi_delete' => 'true'
]);
}
/**
* @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);
}
}