496 lines
16 KiB
PHP
Executable File
496 lines
16 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 GapiAdmins.php
|
|
*/
|
|
|
|
# core
|
|
use IR\Core\Application as Application;
|
|
|
|
# mvc
|
|
use IR\Mvc\Controller as Controller;
|
|
|
|
# models
|
|
|
|
use IR\App\Models\Admin\GapiAdmin as GapiAdmin;
|
|
use IR\App\Models\Admin\GapiUser as GapiUser;
|
|
|
|
# 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 GapiAdmins
|
|
* @description GapiAdmins Controller
|
|
*/
|
|
class GapiAdmins 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',
|
|
'status',
|
|
'email',
|
|
'users',
|
|
'created_date'
|
|
];
|
|
|
|
# creating the html part of the list
|
|
$columns = Page::createTableHeader($columnsArray);
|
|
$filters = Page::createTableFilters($columnsArray);
|
|
|
|
# set menu status
|
|
$this->masterView->set([
|
|
'gapi_management' => 'true',
|
|
//'gapi_servers' => 'true',
|
|
'gapi_admins_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',
|
|
'status',
|
|
'email',
|
|
'users_count' => 'users',
|
|
'created_date'
|
|
];
|
|
|
|
# fetching the results to create the ajax list
|
|
die(json_encode(DataTable::init2($data,'admin.gapi_admin s',$columns,new GapiAdmin(),'gapi-admins','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([
|
|
'gapi_management' => 'true',
|
|
'gapi_servers' => 'true',
|
|
'gapi_servers_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;
|
|
$gapiAdmin = new GapiAdmin();
|
|
$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 !';
|
|
$gapiAdmin->setId(intval($this->app->utils->arrays->get($data,'id')));
|
|
$gapiAdmin->load();
|
|
$gapiAdmin->setLastUpdatedBy($username);
|
|
$gapiAdmin->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 !';
|
|
$gapiAdmin->setCreatedBy($username);
|
|
$gapiAdmin->setCreatedDate(date('Y-m-d'));
|
|
$gapiAdmin->setLastUpdatedBy($username);
|
|
$gapiAdmin->setLastUpdatedDate(date('Y-m-d'));
|
|
$gapiAdmin->setUsersCount(0);
|
|
|
|
}
|
|
|
|
$credentials = json_decode($this->app->utils->arrays->get($data,'gapi-credentials'),true);
|
|
if(isset($credentials['web']))
|
|
{
|
|
|
|
|
|
if($update && trim($this->app->utils->arrays->get($data,'gapi-name'))!=$gapiAdmin->getName()){
|
|
|
|
$this->app->database('system')->execute("UPDATE admin.gapi_users SET admin_name = '".trim($this->app->utils->arrays->get($data,'gapi-name'))."' where admin_id = ".$gapiAdmin->getId().";");
|
|
|
|
}
|
|
$gapiAdmin->setName(trim($this->app->utils->arrays->get($data,'gapi-name')));
|
|
$gapiAdmin->setStatus($this->app->utils->arrays->get($data,'gapi-status','Activated'));
|
|
$gapiAdmin->setEmail(str_replace(['\n','\r','"',"'"], '',trim($this->app->utils->arrays->get($data,'gapi-email'))));
|
|
$domain=explode("@", strtolower(str_replace(['\n','\r','"',"'"], '',trim($this->app->utils->arrays->get($data,'gapi-email')))))[1];
|
|
$gapiAdmin->setDomain($domain);
|
|
$gapiAdmin->setCredential(json_encode($credentials));
|
|
|
|
|
|
|
|
|
|
$result = $update == false ? $gapiAdmin->insert() : $gapiAdmin->update();
|
|
if($result > -1)
|
|
{
|
|
/*
|
|
$this->app->utils->fileSystem->createDir(STORAGE_PATH. DS . 'credentials');
|
|
$this->app->utils->fileSystem->createDir(STORAGE_PATH. DS . 'credentials'. DS . 'tokens');
|
|
$this->app->utils->fileSystem->createDir(STORAGE_PATH. DS . 'credentials'. DS .trim($this->app->utils->arrays->get($data,'gapi-email')) );
|
|
$credentials = json_encode($credentials,JSON_PRETTY_PRINT,JSON_UNESCAPED_UNICODE);
|
|
$this->app->utils->fileSystem->writeFile(STORAGE_PATH. DS . 'credentials' . DS . trim($this->app->utils->arrays->get($data,'gapi-email')). DS .'credentials.json',$credentials);
|
|
*/
|
|
$flag = 'success';
|
|
}
|
|
|
|
|
|
}else{
|
|
$message = 'Credentials 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;
|
|
}
|
|
|
|
$Account = GapiAdmin::first(GapiAdmin::FETCH_ARRAY,['id = ?',$id]);
|
|
|
|
if(count($Account) == 0)
|
|
{
|
|
$valid = false;
|
|
}
|
|
|
|
if($valid == true)
|
|
{
|
|
# preparing the columns array to create the list
|
|
$columnsArray = [
|
|
'id',
|
|
'email',
|
|
'status',
|
|
'created_date'
|
|
|
|
];
|
|
|
|
# creating the html part of the list
|
|
$columns = '';
|
|
$filters = '';
|
|
|
|
foreach ($columnsArray as $column)
|
|
{
|
|
if($column != 'id')
|
|
{
|
|
$columns .= '<th>' . ucwords(str_replace('_',' ',strtolower($column))) . '</th>' . PHP_EOL;
|
|
|
|
if(strpos($column,'_date') > -1 || strpos($column,'_time') > -1)
|
|
{
|
|
$filters .= '<td> <div id="' . $column . '_range" class="input-group date-range-picker"> <input type="text" class="form-control form-filter" name="' . $column . '_range"> <span class="input-group-btn"> <button class="btn default date-range-toggle" type="button"> <i class="fa fa-calendar"></i> </button> </span> </div> </td>' . PHP_EOL;
|
|
}
|
|
else
|
|
{
|
|
if($column == 'status')
|
|
{
|
|
$filters .= '<td> <select name="status" class="form-control form-filter input-sm"> <option value="" selected>All</option> <option value="Activated">Activated</option> <option value="Inactivated">Inactivated</option> </select> </td>' . PHP_EOL;
|
|
}
|
|
else
|
|
{
|
|
$filters .= '<td><input type="text" class="form-control form-filter" name="' . $column . '"></td>' . PHP_EOL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$credentials='';
|
|
/*
|
|
if($this->app->utils->fileSystem->fileExists(STORAGE_PATH. DS . 'credentials' . DS . $Account["email"].DS .'credentials.json')){
|
|
$credentials=json_encode(json_decode($this->app->utils->fileSystem->readFile(STORAGE_PATH. DS . 'credentials' . DS . $Account["email"].DS .'credentials.json'),true),JSON_PRETTY_PRINT);
|
|
|
|
$credentials=str_replace("\/", "/", $credentials);
|
|
}
|
|
*/
|
|
if($Account['credential']!=""){
|
|
$credentials=json_encode(json_decode($Account['credential'],true),JSON_PRETTY_PRINT);
|
|
|
|
$credentials=str_replace("\/", "/", $credentials);
|
|
}
|
|
|
|
|
|
# set data to the page view
|
|
$this->pageView->set([
|
|
'Account' => $Account,
|
|
'credentials' => $credentials,
|
|
'columns' => $columns,
|
|
'filters' => $filters
|
|
]);
|
|
}
|
|
else
|
|
{
|
|
# stores the message in the session
|
|
Page::registerMessage('error','Invalid GApi 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([
|
|
'gapi_management' => 'true',
|
|
'gapi_servers' => 'true',
|
|
'gapi_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',
|
|
'status',
|
|
'created_date'
|
|
|
|
];
|
|
|
|
# fetching the results to create the ajax list
|
|
$query = $this->app->database('system')->query()->from('admin.gapi_users',$columns)->where('admin_id = ?',$accountId);
|
|
|
|
die(json_encode(DataTable::init($data,'admin.gapi_users s',$columns,new GapiUser(),'gapi-users','DESC',$query)));
|
|
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
} |