mirror of
https://github.com/newnius/YAO-portal.git
synced 2025-06-06 07:11:54 +00:00
add workspace
This commit is contained in:
parent
d0a4b891b5
commit
3a22e99602
@ -10,7 +10,7 @@ class AgentManager
|
||||
private static $table = 'yao_agent';
|
||||
|
||||
/*
|
||||
* do add link
|
||||
* do add agent
|
||||
*/
|
||||
public static function add(CRObject $agent)
|
||||
{
|
||||
|
119
WorkspaceManager.class.php
Executable file
119
WorkspaceManager.class.php
Executable file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
require_once('util4p/CRObject.class.php');
|
||||
require_once('util4p/MysqlPDO.class.php');
|
||||
require_once('util4p/SQLBuilder.class.php');
|
||||
|
||||
class WorkspaceManager
|
||||
{
|
||||
|
||||
private static $table = 'yao_workspace';
|
||||
|
||||
/*
|
||||
* do add workspace
|
||||
*/
|
||||
public static function add(CRObject $workspace)
|
||||
{
|
||||
$name = $workspace->get('name');
|
||||
$content = $workspace->get('content');
|
||||
$virtual_cluster = $workspace->getInt('virtual_cluster');
|
||||
$permission = $workspace->getInt('permission');
|
||||
$created_at = $workspace->getInt('created_at', time());
|
||||
$updated_at = $workspace->getInt('updated_at', time());
|
||||
$created_by = $workspace->get('created_by');
|
||||
|
||||
$key_values = array(
|
||||
'name' => '?', 'content' => '?', 'virtual_cluster' => '?', 'permission' => '?',
|
||||
'created_at' => '?', 'updated_at' => '?', 'created_by' => '?'
|
||||
);
|
||||
$builder = new SQLBuilder();
|
||||
$builder->insert(self::$table, $key_values);
|
||||
$sql = $builder->build();
|
||||
$params = array($name, $content, $virtual_cluster, $permission, $created_at, $updated_at, $created_by);
|
||||
return (new MysqlPDO())->execute($sql, $params);
|
||||
}
|
||||
|
||||
/* */
|
||||
public static function gets(CRObject $rule)
|
||||
{
|
||||
$offset = $rule->getInt('offset', 0);
|
||||
$limit = $rule->getInt('limit', -1);
|
||||
$selected_rows = array();
|
||||
$where = array();
|
||||
$params = array();
|
||||
$order_by = array('id' => 'DESC');
|
||||
$builder = new SQLBuilder();
|
||||
$builder->select(self::$table, $selected_rows);
|
||||
$builder->where($where);
|
||||
$builder->order($order_by);
|
||||
$builder->limit($offset, $limit);
|
||||
$sql = $builder->build();
|
||||
$workspaces = (new MysqlPDO())->executeQuery($sql, $params);
|
||||
return $workspaces;
|
||||
}
|
||||
|
||||
/* */
|
||||
public static function count(CRObject $rule)
|
||||
{
|
||||
$selected_rows = array('COUNT(1) as cnt');
|
||||
$where = array();
|
||||
$params = array();
|
||||
$builder = new SQLBuilder();
|
||||
$builder->select(self::$table, $selected_rows);
|
||||
$builder->where($where);
|
||||
$sql = $builder->build();
|
||||
$res = (new MysqlPDO())->executeQuery($sql, $params);
|
||||
return $res === null ? 0 : intval($res[0]['cnt']);
|
||||
}
|
||||
|
||||
/* get workspace by ip */
|
||||
public static function getByID($id)
|
||||
{
|
||||
$selected_rows = array();
|
||||
$where = array('id' => '?');
|
||||
$params = array($id);
|
||||
$builder = new SQLBuilder();
|
||||
$builder->select(self::$table, $selected_rows);
|
||||
$builder->where($where);
|
||||
$sql = $builder->build();
|
||||
$workspaces = (new MysqlPDO())->executeQuery($sql, $params);
|
||||
return $workspaces !== null && count($workspaces) === 1 ? $workspaces[0] : null;
|
||||
}
|
||||
|
||||
/*
|
||||
* do update workspace
|
||||
*/
|
||||
public static function update(CRObject $workspace)
|
||||
{
|
||||
$id = $workspace->getInt('id');
|
||||
$name = $workspace->get('name');
|
||||
$content = $workspace->get('content');
|
||||
$permission = $workspace->getInt('permission');
|
||||
$updated_at = $workspace->getInt('updated_at', time());
|
||||
|
||||
$key_values = array(
|
||||
'name' => '?', 'content' => '?', 'permission' => '?', 'updated_at' => '?'
|
||||
);
|
||||
$where = array('id' => '?');
|
||||
$builder = new SQLBuilder();
|
||||
$builder->update(self::$table, $key_values);
|
||||
$builder->where($where);
|
||||
$sql = $builder->build();
|
||||
$params = array($name, $content, $permission, $updated_at, $id);
|
||||
return (new MysqlPDO())->execute($sql, $params);
|
||||
}
|
||||
|
||||
/* */
|
||||
public static function remove(CRObject $workspace)
|
||||
{
|
||||
$id = $workspace->getInt('id');
|
||||
$where = array('id' => '?');
|
||||
$builder = new SQLBuilder();
|
||||
$builder->delete(self::$table);
|
||||
$builder->where($where);
|
||||
$sql = $builder->build();
|
||||
$params = array($id);
|
||||
return (new MysqlPDO())->execute($sql, $params);
|
||||
}
|
||||
|
||||
}
|
37
ajax.php
37
ajax.php
@ -9,6 +9,7 @@ require_once('Securer.class.php');
|
||||
require_once('user.logic.php');
|
||||
require_once('job.logic.php');
|
||||
require_once('agent.logic.php');
|
||||
require_once('workspace.logic.php');
|
||||
|
||||
require_once('config.inc.php');
|
||||
require_once('init.inc.php');
|
||||
@ -25,8 +26,6 @@ function csrf_check($action)
|
||||
return false;
|
||||
}
|
||||
$post_methods = array(
|
||||
'job_submit',
|
||||
'job_stop',
|
||||
'signout',
|
||||
'oauth_get_url'
|
||||
);
|
||||
@ -82,7 +81,7 @@ switch ($action) {
|
||||
case 'job_stop':
|
||||
$job = new CRObject();
|
||||
$job->set('id', cr_get_POST('id'));
|
||||
$res = job_stop($link);
|
||||
$res = job_stop($job);
|
||||
break;
|
||||
|
||||
case 'job_describe':
|
||||
@ -112,6 +111,38 @@ switch ($action) {
|
||||
$res = agent_remove($job);
|
||||
break;
|
||||
|
||||
case 'workspace_list':
|
||||
$rule = new CRObject();
|
||||
$rule->set('offset', cr_get_GET('offset'));
|
||||
$rule->set('limit', cr_get_GET('limit'));
|
||||
$res = workspace_list($rule);
|
||||
break;
|
||||
|
||||
case 'workspace_add':
|
||||
$workspace = new CRObject();
|
||||
$workspace->set('name', cr_get_POST('name'));
|
||||
$workspace->set('content', cr_get_POST('content'));
|
||||
$workspace->set('virtual_cluster', cr_get_POST('virtual_cluster'));
|
||||
$workspace->set('permission', cr_get_POST('permission'));
|
||||
$res = workspace_add($workspace);
|
||||
break;
|
||||
|
||||
case 'workspace_update':
|
||||
$workspace = new CRObject();
|
||||
$workspace->set('id', cr_get_POST('id'));
|
||||
$workspace->set('name', cr_get_POST('name'));
|
||||
$workspace->set('content', cr_get_POST('content'));
|
||||
$workspace->set('virtual_cluster', cr_get_POST('virtual_cluster'));
|
||||
$workspace->set('permission', cr_get_POST('permission'));
|
||||
$res = workspace_update($workspace);
|
||||
break;
|
||||
|
||||
case 'workspace_remove':
|
||||
$workspace = new CRObject();
|
||||
$workspace->set('id', cr_get_POST('id'));
|
||||
$res = workspace_remove($workspace);
|
||||
break;
|
||||
|
||||
case 'user_signout':
|
||||
$res = user_signout();
|
||||
break;
|
||||
|
@ -75,6 +75,12 @@ function init_accessMap()
|
||||
'agent.add' => array('root', 'admin'),
|
||||
'agent.remove' => array('root', 'admin'),
|
||||
|
||||
/* workspace */
|
||||
'workspace.list' => array('root', 'admin', 'normal'),
|
||||
'workspace.add' => array('root', 'admin', 'normal'),
|
||||
'workspace.update' => array('root', 'admin', 'normal'),
|
||||
'workspace.remove' => array('root', 'admin', 'normal'),
|
||||
|
||||
/* ucenter entry show control */
|
||||
'ucenter.home' => array('root', 'admin', 'developer', 'normal'),
|
||||
'ucenter.jobs' => array('root', 'admin', 'developer', 'normal'),
|
||||
|
@ -64,6 +64,7 @@ function create_table_workspace()
|
||||
`name` varchar(64) NOT NULL,
|
||||
`content` json NOT NULL,
|
||||
`created_at` BIGINT NOT NULL,
|
||||
`updated_at` BIGINT NOT NULL,
|
||||
`virtual_cluster` varchar(64) NOT NULL,
|
||||
INDEX(`virtual_cluster`),
|
||||
`created_by` int NOT NULL,
|
||||
|
42
modals.php
42
modals.php
@ -188,4 +188,46 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- workspace modal -->
|
||||
<div class="modal fade" id="modal-workspace" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
|
||||
aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content panel-info">
|
||||
<div class="modal-header panel-heading">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
|
||||
</button>
|
||||
<h4 id="modal-workspace-title" class="modal-title">Add New Workspace</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form class="form" action="javascript:void(0)">
|
||||
<label>Name</label>
|
||||
<div class="form-group form-group-lg">
|
||||
<label for="form-agent-ip" class="sr-only">IP</label>
|
||||
<input type="text" id="form-workspace-name" class="form-control" maxlength="64"
|
||||
placeholder="" required/>
|
||||
</div>
|
||||
<label>Virtual Cluster</label>
|
||||
<div class="form-group form-group-lg">
|
||||
<label for="form-workspace-virtual-cluster" class="sr-only">Virtual Cluster</label>
|
||||
<select id="form-workspace-virtual-cluster" class="form-control">
|
||||
<option value="0">default</option>
|
||||
</select>
|
||||
</div>
|
||||
<label>Permission</label>
|
||||
<div class="form-group form-group-lg">
|
||||
<label for="form-workspace-permission" class="sr-only">Token</label>
|
||||
<input type="number" id="form-workspace-permission" class="form-control" placeholder=""/>
|
||||
</div>
|
||||
<div>
|
||||
<input type="hidden" id="form-workspace-submit-type"/>
|
||||
<input type="hidden" id="form-workspace-id"/>
|
||||
<input type="hidden" id="form-workspace-content"/>
|
||||
<button id="form-workspace-submit" type="submit" class="btn btn-primary btn-lg">Submit</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
@ -15,6 +15,10 @@ $(function () {
|
||||
register_events_agent();
|
||||
load_agents('');
|
||||
break;
|
||||
case "workspaces":
|
||||
register_events_workspace();
|
||||
load_workspaces('');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
157
static/workspace.js
Executable file
157
static/workspace.js
Executable file
@ -0,0 +1,157 @@
|
||||
function register_events_workspace() {
|
||||
$('#btn-workspace-add').click(function (e) {
|
||||
$('#form-workspace-submit-type').val('add');
|
||||
$('#modal-workspace').modal('show');
|
||||
});
|
||||
|
||||
$("#form-workspace-submit").click(function (e) {
|
||||
var id = $('#form-workspace-id').val();
|
||||
var name = $('#form-workspace-name').val();
|
||||
var content = $('#form-workspace-content').val();
|
||||
var virtual_cluster = $('#form-workspace-virtual-cluster').val();
|
||||
var permission = $('#form-workspace-permission').val();
|
||||
|
||||
/* TODO validate form */
|
||||
|
||||
$('#modal-workspace').modal('hide');
|
||||
var action = 'workspace_add';
|
||||
if ($('#form-workspace-submit-type').val() !== 'add')
|
||||
action = 'workspace_update';
|
||||
|
||||
var ajax = $.ajax({
|
||||
url: window.config.BASE_URL + "/service?action=" + action,
|
||||
type: 'POST',
|
||||
data: {
|
||||
id: id,
|
||||
name: name,
|
||||
content: "[]",
|
||||
virtual_cluster: virtual_cluster,
|
||||
permission: permission
|
||||
}
|
||||
});
|
||||
ajax.done(function (res) {
|
||||
if (res["errno"] !== 0) {
|
||||
$("#modal-msg-content").html(res["msg"]);
|
||||
$("#modal-msg").modal('show');
|
||||
}
|
||||
$('#table-workspace').bootstrapTable("refresh");
|
||||
|
||||
});
|
||||
ajax.fail(function (jqXHR, textStatus) {
|
||||
$("#modal-msg-content").html("Request failed : " + jqXHR.statusText);
|
||||
$("#modal-msg").modal('show');
|
||||
$('#table-workspace').bootstrapTable("refresh");
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function load_workspaces(cluster) {
|
||||
$("#table-workspace").bootstrapTable({
|
||||
url: window.config.BASE_URL + '/service?action=workspace_list&who=' + cluster,
|
||||
responseHandler: workspaceResponseHandler,
|
||||
sidePagination: 'server',
|
||||
cache: true,
|
||||
striped: true,
|
||||
pagination: true,
|
||||
pageSize: 10,
|
||||
pageList: [10, 25, 50, 100, 200],
|
||||
search: false,
|
||||
showColumns: true,
|
||||
showRefresh: true,
|
||||
showToggle: false,
|
||||
showPaginationSwitch: true,
|
||||
minimumCountColumns: 2,
|
||||
clickToSelect: false,
|
||||
sortName: 'nobody',
|
||||
sortOrder: 'desc',
|
||||
smartDisplay: true,
|
||||
mobileResponsive: true,
|
||||
showExport: true,
|
||||
columns: [{
|
||||
field: 'id',
|
||||
title: 'ID',
|
||||
align: 'center',
|
||||
valign: 'middle',
|
||||
visible: false
|
||||
}, {
|
||||
field: 'name',
|
||||
title: 'Name',
|
||||
align: 'center',
|
||||
valign: 'middle',
|
||||
escape: true
|
||||
}, {
|
||||
field: 'virtual_cluster',
|
||||
title: 'Virtual Cluster',
|
||||
align: 'center',
|
||||
valign: 'middle'
|
||||
}, {
|
||||
field: 'permission',
|
||||
title: 'Permission',
|
||||
align: 'center',
|
||||
valign: 'middle'
|
||||
}, {
|
||||
field: 'operate',
|
||||
title: 'Operate',
|
||||
align: 'center',
|
||||
events: workspaceOperateEvents,
|
||||
formatter: workspaceOperateFormatter
|
||||
}]
|
||||
});
|
||||
}
|
||||
|
||||
function workspaceResponseHandler(res) {
|
||||
if (res['errno'] === 0) {
|
||||
var tmp = {};
|
||||
tmp["total"] = res["count"];
|
||||
tmp["rows"] = res["workspaces"];
|
||||
return tmp;
|
||||
}
|
||||
$("#modal-msg-content").html(res["msg"]);
|
||||
$("#modal-msg").modal('show');
|
||||
return [];
|
||||
}
|
||||
|
||||
function workspaceOperateFormatter(value, row, index) {
|
||||
var div = '<div class="btn-group" role="group" aria-label="...">';
|
||||
div += '<button class="btn btn-default view"><i class="glyphicon glyphicon-eye-open"></i> </button>';
|
||||
div += '<button class="btn btn-default edit"><i class="glyphicon glyphicon-edit"></i> </button>';
|
||||
div += '<button class="btn btn-default remove"><i class="glyphicon glyphicon-remove"></i> </button>';
|
||||
div += '</div>';
|
||||
return div;
|
||||
}
|
||||
|
||||
window.workspaceOperateEvents = {
|
||||
'click .view': function (e, value, row, index) {
|
||||
$('#form-workspace-id').val(row.id);
|
||||
$('#form-workspace-submit-type').val('view');
|
||||
$('#modal-workspace').modal('show');
|
||||
},
|
||||
'click .edit': function (e, value, row, index) {
|
||||
$('#form-workspace-id').val(row.id);
|
||||
$('#form-workspace-submit-type').val('view');
|
||||
$('#modal-workspace').modal('show');
|
||||
},
|
||||
'click .remove': function (e, value, row, index) {
|
||||
if (!confirm('Are you sure to remove this workspace?')) {
|
||||
return;
|
||||
}
|
||||
var ajax = $.ajax({
|
||||
url: window.config.BASE_URL + "/service?action=workspace_remove",
|
||||
type: 'POST',
|
||||
data: {id: row.id}
|
||||
});
|
||||
ajax.done(function (res) {
|
||||
if (res["errno"] !== 0) {
|
||||
$("#modal-msg-content").html(res["msg"]);
|
||||
$("#modal-msg").modal('show');
|
||||
}
|
||||
$('#table-workspace').bootstrapTable("refresh");
|
||||
});
|
||||
ajax.fail(function (jqXHR, textStatus) {
|
||||
$("#modal-msg-content").html("Request failed : " + jqXHR.statusText);
|
||||
$("#modal-msg").modal('show');
|
||||
$('#table-workspace').bootstrapTable("refresh");
|
||||
});
|
||||
}
|
||||
};
|
22
ucenter.php
22
ucenter.php
@ -39,6 +39,9 @@ if (isset($_GET['logs'])) {
|
||||
} elseif (isset($_GET['agents'])) {
|
||||
$page_type = 'agents';
|
||||
|
||||
} elseif (isset($_GET['workspaces'])) {
|
||||
$page_type = 'workspaces';
|
||||
|
||||
} elseif (isset($_GET['home'])) {
|
||||
$page_type = 'home';
|
||||
|
||||
@ -190,6 +193,24 @@ foreach ($entries as $entry) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php } elseif ($page_type === 'workspaces') { ?>
|
||||
<div id="workspaces">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Workspaces</div>
|
||||
<div class="panel-body">
|
||||
<div class="table-responsive">
|
||||
<div id="toolbar">
|
||||
<button id="btn-workspace-add" class="btn btn-primary">
|
||||
<i class="glyphicon glyphicon-plus"></i> New
|
||||
</button>
|
||||
</div>
|
||||
<table id="table-workspace" data-toolbar="#toolbar" class="table table-striped">
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php } ?>
|
||||
|
||||
</div>
|
||||
@ -208,6 +229,7 @@ foreach ($entries as $entry) {
|
||||
|
||||
<script src="static/job.js"></script>
|
||||
<script src="static/agent.js"></script>
|
||||
<script src="static/workspace.js"></script>
|
||||
<script src="static/ucenter.js"></script>
|
||||
</body>
|
||||
</html>
|
79
workspace.logic.php
Normal file
79
workspace.logic.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
require_once('predis/autoload.php');
|
||||
|
||||
require_once('util4p/util.php');
|
||||
require_once('util4p/CRObject.class.php');
|
||||
require_once('util4p/AccessController.class.php');
|
||||
require_once('util4p/CRLogger.class.php');
|
||||
|
||||
require_once('Code.class.php');
|
||||
require_once('WorkspaceManager.class.php');
|
||||
|
||||
require_once('config.inc.php');
|
||||
require_once('init.inc.php');
|
||||
|
||||
function workspace_add(CRObject $workspace)
|
||||
{
|
||||
if (!AccessController::hasAccess(Session::get('role', 'visitor'), 'agent.add')) {
|
||||
$res['errno'] = Code::NO_PRIVILEGE;
|
||||
return $res;
|
||||
}
|
||||
if (WorkspaceManager::getByID($workspace->get('ip')) !== null) {
|
||||
$res['errno'] = Code::RECORD_ALREADY_EXIST;
|
||||
} else {
|
||||
$workspace->set('created_by', Session::get('uid'));
|
||||
$res['errno'] = WorkspaceManager::add($workspace) ? Code::SUCCESS : Code::UNKNOWN_ERROR;
|
||||
}
|
||||
$log = new CRObject();
|
||||
$log->set('scope', Session::get('uid'));
|
||||
$log->set('tag', 'workspace.add');
|
||||
$content = array('workspace' => $workspace, 'response' => $res['errno']);
|
||||
$log->set('content', json_encode($content));
|
||||
CRLogger::log($log);
|
||||
return $res;
|
||||
}
|
||||
|
||||
function workspace_remove(CRObject $workspace)
|
||||
{
|
||||
if (!AccessController::hasAccess(Session::get('role', 'visitor'), 'workspace.remove')) {
|
||||
$res['errno'] = Code::NO_PRIVILEGE;
|
||||
return $res;
|
||||
}
|
||||
$res['errno'] = WorkspaceManager::remove($workspace) ? Code::SUCCESS : Code::UNKNOWN_ERROR;
|
||||
$log = new CRObject();
|
||||
$log->set('scope', Session::get('uid'));
|
||||
$log->set('tag', 'workspace.remove');
|
||||
$content = array('workspace' => $workspace, 'response' => $res['errno']);
|
||||
$log->set('content', json_encode($content));
|
||||
CRLogger::log($log);
|
||||
return $res;
|
||||
}
|
||||
|
||||
function workspace_update(CRObject $workspace)
|
||||
{
|
||||
if (!AccessController::hasAccess(Session::get('role', 'visitor'), 'workspace.update')) {
|
||||
$res['errno'] = Code::NO_PRIVILEGE;
|
||||
return $res;
|
||||
}
|
||||
$res['errno'] = WorkspaceManager::update($workspace) ? Code::SUCCESS : Code::UNKNOWN_ERROR;
|
||||
$log = new CRObject();
|
||||
$log->set('scope', Session::get('uid'));
|
||||
$log->set('tag', 'workspace.update');
|
||||
$content = array('workspace' => $workspace, 'response' => $res['errno']);
|
||||
$log->set('content', json_encode($content));
|
||||
CRLogger::log($log);
|
||||
return $res;
|
||||
}
|
||||
|
||||
function workspace_list(CRObject $rule)
|
||||
{
|
||||
if (!AccessController::hasAccess(Session::get('role', 'visitor'), 'workspace.list')) {
|
||||
$res['errno'] = Code::NO_PRIVILEGE;
|
||||
return $res;
|
||||
}
|
||||
$res['workspaces'] = WorkspaceManager::gets($rule);
|
||||
$res['count'] = WorkspaceManager::count($rule);
|
||||
$res['errno'] = $res['workspaces'] === null ? Code::FAIL : Code::SUCCESS;
|
||||
return $res;
|
||||
}
|
Loading…
Reference in New Issue
Block a user