Permalink
Browse files

Extend Task controller to manage tasks from admin panel.

  • Loading branch information...
1 parent 6e2b598 commit 1b905b77acc583da0d3251ab042d1b6179e087d7 @cdujeu cdujeu committed Jul 28, 2016
@@ -10,6 +10,14 @@
return this._internal['id'];
}
+ getUserId(){
+ return this._internal['userId'];
+ }
+
+ getWorkspaceId(){
+ return this._internal['wsId'];
+ }
+
isStoppable(){
return this._internal['flags'] & Task.FLAG_STOPPABLE;
}
@@ -64,25 +72,47 @@
class TaskAPI{
- static createTask(task){
- PydioApi.getClient().request({
+ static createTask(task, targetUsers = null, targetRepositories = null){
+ let params = {
"get_action":"task_create",
"task":JSON.stringify(task.getData())
- });
+ };
+ if(targetUsers && targetRepositories){
+ params['target-users'] = targetUsers;
+ params['target-repositories'] = targetRepositories;
+ }
+ console.log(params);
+ PydioApi.getClient().request(params);
}
- static loadTasks(callback){
- PydioApi.getClient().request({
- "get_action":"tasks_list"
- }, function(transport){
+ static loadTasks(callback, params = null){
+ if(params){
+ params['get_action'] = 'tasks_list';
+ }else{
+ params = {get_action:'tasks_list'};
+ }
+ PydioApi.getClient().request(params, function(transport){
if(transport.responseJSON){
let tasks = transport.responseJSON.map(function(taskData){
return new Task(taskData);
});
callback(tasks);
}
- }.bind(this));
+ }.bind(this), null, {discrete: true});
+ }
+ static loadAdminTasks(userScope = null, repoScope = null, callback){
+ let params = {get_action:"tasks_list"};
+ if(userScope){
+ params["scope"] = "user";
+ params["user_id"] = userScope;
+ }else if(repoScope){
+ params["scope"] = "repository";
+ params["repo_id"] = repoScope;
+ }else{
+ params["scope"] = "global";
+ }
+ TaskAPI.loadTasks(callback, params);
}
static updateTaskStatus(task, status){
@@ -172,7 +202,7 @@
return this._tasksList;
}
- static enqueueActionTask(label, action, parameters = {}, nodes = [], flags = Task.FLAG_STOPPABLE){
+ static enqueueActionTask(label, action, parameters = {}, nodes = [], flags = Task.FLAG_STOPPABLE, targetUsers = null, targetRepositories = null){
let task = {
label: label,
flags: flags,
@@ -182,7 +212,7 @@
parameters: parameters,
nodes: nodes
};
- TaskAPI.createTask(new Task(task));
+ TaskAPI.createTask(new Task(task), targetUsers, targetRepositories);
}
static enqueueLocalTask(task){
@@ -232,9 +262,19 @@
});
var TaskEntry = React.createClass({
+
+ propTypes: {
+ task: React.PropTypes.instanceOf(Task),
+ adminDisplayScope: React.PropTypes.bool
+ },
+
render: function(){
let t = this.props.task;
let actions;
+ let scopeInfo;
+ if(this.props['adminDisplayScope'] && this.props.adminDisplayScope === 'repository'){
+ scopeInfo = "["+ this.props.task.getUserId() +"] ";
+ }
if(t.getStatus() == Task.STATUS_RUNNING){
if(t.isStoppable()){
actions = (<span className="icon-stop" onClick={t.pause.bind(t)}/>);
@@ -252,7 +292,7 @@
return (
<div className="task">
<div className="task_texts">
- <div className="task_label">{t.getLabel()}</div>
+ <div className="task_label">{scopeInfo}{t.getLabel()}</div>
<div className="status_message" title={t.getStatusMessage()}>{t.getStatusMessage()}</div>
</div>
<TaskAction task={t}/>
@@ -311,7 +351,9 @@
var ns = global.PydioTasks || {};
ns.Store = TaskStore;
ns.Task = Task;
+ ns.API = TaskAPI;
ns.Panel = TasksPanel;
+ ns.TaskEntry = TaskEntry;
global.PydioTasks = ns;
})(window);
@@ -76,7 +76,7 @@ public function getScheduledTasks();
* @param RepositoryInterface $repository
* @return Task[]
*/
- public function getCurrentRunningTasks($user, $repository);
+ public function getCurrentRunningTasks($user = null, $repository = null);
/**
* @param AJXP_Node $node
@@ -120,9 +120,9 @@ public function getTasks($user = null, $repository = null, $status = -1, $schedu
/**
* @param AbstractUser $user
* @param Repository $repository
- * @return Task[]
+ * @return \Pydio\Tasks\Task[]
*/
- public function getCurrentRunningTasks($user, $repository)
+ public function getCurrentRunningTasks($user = null, $repository = null)
{
// TODO: Implement getCurrentRunningTasks() method.
}
@@ -180,14 +180,18 @@ public function getChildrenTasks($taskId){
/**
* @param UserInterface $user
* @param RepositoryInterface $repository
- * @return Task[]
+ * @return \Pydio\Tasks\Task[]
*/
- public function getCurrentRunningTasks($user, $repository)
+ public function getCurrentRunningTasks($user = null, $repository = null)
{
$tasks = [];
$where = [];
- $where[] = array("[userId] = %s", $user->getId());
- $where[] = array("[wsId] = %s", $repository->getId());
+ if($user !== null){
+ $where[] = array("[userId] = %s", $user->getId());
+ }
+ if($repository !== null){
+ $where[] = array("[wsId] = %s", $repository->getId());
+ }
$where[] = array("[status] IN (1,2,8,16)");
$res = \dibi::query('SELECT * FROM [ajxp_tasks] WHERE %and', $where);
foreach ($res->fetchAll() as $row) {
@@ -32,6 +32,8 @@
use Pydio\Core\PluginFramework\SqlTableProvider;
+use Pydio\Core\Services\RepositoryService;
+use Pydio\Core\Services\UsersService;
use Pydio\Core\Utils\DBHelper;
use Pydio\Core\Utils\Vars\OptionsHelper;
use Pydio\Core\Utils\Vars\StringHelper;
@@ -57,6 +59,7 @@ public function init(ContextInterface $ctx, $options = [])
protected function initTaskFromApi(ServerRequestInterface $request, Task &$task){
$params = $request->getParsedBody();
$taskData = json_decode($params["task"], true);
+ /** @var Task $task */
$task = SimpleRestResourceRouter::cast($task, $taskData);
if(isSet($params["taskId"])) {
$task->setId($params["taskId"]);
@@ -65,8 +68,15 @@ protected function initTaskFromApi(ServerRequestInterface $request, Task &$task)
}
/** @var ContextInterface $ctx */
$ctx = $request->getAttribute("ctx");
- $task->setUserId($ctx->getUser()->getId());
- $task->setWsId($ctx->getRepositoryId());
+
+ if(isSet($params["target-users"]) && isSet($params["target-repositories"]) && $ctx->getUser()->isAdmin()){
+ $task->setUserId($ctx->getUser()->getId());
+ $task->setImpersonateUsers($params["target-users"]);
+ $task->setWsId($params["target-repositories"]);
+ }else{
+ $task->setUserId($ctx->getUser()->getId());
+ $task->setWsId($ctx->getRepositoryId());
+ }
if(count($task->nodes)){
foreach($task->nodes as $index => $path){
$task->nodes[$index] = "pydio://".$task->getWsId().$path;
@@ -86,17 +96,30 @@ public function route(ServerRequestInterface &$request, ResponseInterface &$resp
switch ($action){
case "tasks_list":
-
- $tasks = $taskService->getCurrentRunningTasks($ctx->getUser(), $ctx->getRepository());
+
+ $params = $request->getParsedBody();
+ if(isSet($params["scope"]) && $ctx->getUser()->isAdmin()){
+ $userObject = $repoObject = null;
+ if($params["scope"] === "repository" && !empty($params["repository_id"])){
+ $repoObject = RepositoryService::getRepositoryById($params["repository_id"]);
+ }else if($params["scope"] === "user" && !empty($params["user_id"])){
+ $userObject = UsersService::getUserById($params["user_id"]);
+ }
+ $tasks = $taskService->getCurrentRunningTasks($userObject, $repoObject);
+ }else{
+ $tasks = $taskService->getCurrentRunningTasks($ctx->getUser(), $ctx->getRepository());
+ }
$response = new JsonResponse($tasks);
break;
case "task_info":
+
$task = $taskService->getTaskById($request->getParsedBody()["taskId"]);
$response = new JsonResponse($task);
break;
case "task_create":
+
$newTask = new Task();
$this->initTaskFromApi($request, $newTask);
$taskService->createTask($newTask, $newTask->getSchedule());
@@ -107,6 +130,7 @@ public function route(ServerRequestInterface &$request, ResponseInterface &$resp
break;
case "task_update":
+
$taskData = $request->getParsedBody()["request_body"];
$newTask = $taskService->getTaskById($request->getParsedBody()["taskId"]);
$newTask = SimpleRestResourceRouter::cast($newTask, $taskData);
@@ -115,6 +139,7 @@ public function route(ServerRequestInterface &$request, ResponseInterface &$resp
break;
case "task_toggle_status":
+
$task = $taskService->getTaskById($request->getParsedBody()["taskId"]);
if(!empty($task)){
$status = intval($request->getParsedBody()["status"]);
@@ -252,7 +252,7 @@ public function getPendingTasks()
/**
* @inheritdoc
*/
- public function getCurrentRunningTasks($user, $repository)
+ public function getCurrentRunningTasks($user = null, $repository = null)
{
return $this->realProvider->getCurrentRunningTasks($user, $repository);
}

0 comments on commit 1b905b7

Please sign in to comment.