1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/kanboard_ynh.git synced 2024-09-03 19:36:17 +02:00
kanboard_ynh/sources/app/Model/Acl.php

204 lines
5.6 KiB
PHP
Raw Normal View History

2014-07-20 12:26:15 +02:00
<?php
namespace Model;
/**
2015-01-16 14:23:05 +01:00
* Access List
2014-07-20 12:26:15 +02:00
*
* @package model
* @author Frederic Guillot
*/
class Acl extends Base
{
/**
* Controllers and actions allowed from outside
*
* @access private
* @var array
*/
2015-01-16 14:23:05 +01:00
private $public_acl = array(
2014-07-20 12:26:15 +02:00
'user' => array('login', 'check', 'google', 'github'),
2014-11-23 20:13:38 +01:00
'task' => array('readonly'),
2014-07-20 12:26:15 +02:00
'board' => array('readonly'),
2014-10-22 19:59:09 +02:00
'project' => array('feed'),
2015-01-16 14:23:05 +01:00
'webhook' => '*',
2015-02-25 17:29:06 +01:00
'app' => array('colors'),
2014-07-20 12:26:15 +02:00
);
/**
2015-01-16 14:23:05 +01:00
* Controllers and actions for project members
2014-07-20 12:26:15 +02:00
*
* @access private
* @var array
*/
2015-01-16 14:23:05 +01:00
private $member_acl = array(
'board' => '*',
'comment' => '*',
'file' => '*',
'project' => array('show', 'tasks', 'search', 'activity'),
'subtask' => '*',
'task' => '*',
2015-02-25 17:29:06 +01:00
'tasklink' => '*',
'calendar' => array('show', 'project'),
2014-07-20 12:26:15 +02:00
);
/**
2015-01-16 14:23:05 +01:00
* Controllers and actions for project managers
2014-07-20 12:26:15 +02:00
*
2015-01-16 14:23:05 +01:00
* @access private
* @var array
2014-07-20 12:26:15 +02:00
*/
2015-01-16 14:23:05 +01:00
private $manager_acl = array(
'action' => '*',
'analytic' => '*',
2015-02-25 17:29:06 +01:00
'board' => array('movecolumn', 'edit', 'editcolumn', 'updatecolumn', 'add', 'remove'),
2015-01-16 14:23:05 +01:00
'category' => '*',
'export' => array('tasks', 'subtasks', 'summary'),
'project' => array('edit', 'update', 'share', 'integration', 'users', 'alloweverybody', 'allow', 'setowner', 'revoke', 'duplicate', 'disable', 'enable'),
'swimlane' => '*',
);
2014-07-20 12:26:15 +02:00
2015-01-16 14:23:05 +01:00
/**
* Controllers and actions for admins
*
* @access private
* @var array
*/
private $admin_acl = array(
2015-02-25 17:29:06 +01:00
'app' => array('dashboard'),
2015-01-16 14:23:05 +01:00
'user' => array('index', 'create', 'save', 'remove'),
'config' => '*',
2015-02-25 17:29:06 +01:00
'link' => '*',
2015-01-16 14:23:05 +01:00
'project' => array('remove'),
);
2014-07-20 12:26:15 +02:00
/**
2015-01-16 14:23:05 +01:00
* Return true if the specified controller/action match the given acl
2014-07-20 12:26:15 +02:00
*
* @access public
2015-01-16 14:23:05 +01:00
* @param array $acl Acl list
2014-07-20 12:26:15 +02:00
* @param string $controller Controller name
* @param string $action Action name
* @return bool
*/
2015-01-16 14:23:05 +01:00
public function matchAcl(array $acl, $controller, $action)
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
$action = strtolower($action);
return isset($acl[$controller]) && $this->hasAction($action, $acl[$controller]);
2014-07-20 12:26:15 +02:00
}
/**
2015-01-16 14:23:05 +01:00
* Return true if the specified action is inside the list of actions
2014-07-20 12:26:15 +02:00
*
* @access public
* @param string $action Action name
2015-01-16 14:23:05 +01:00
* @param mixed $action Actions list
2014-07-20 12:26:15 +02:00
* @return bool
*/
2015-01-16 14:23:05 +01:00
public function hasAction($action, $actions)
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
if (is_array($actions)) {
return in_array($action, $actions);
}
return $actions === '*';
2014-07-20 12:26:15 +02:00
}
/**
2015-01-16 14:23:05 +01:00
* Return true if the given action is public
2014-07-20 12:26:15 +02:00
*
* @access public
2015-01-16 14:23:05 +01:00
* @param string $controller Controller name
* @param string $action Action name
2014-07-20 12:26:15 +02:00
* @return bool
*/
2015-01-16 14:23:05 +01:00
public function isPublicAction($controller, $action)
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
return $this->matchAcl($this->public_acl, $controller, $action);
2014-07-20 12:26:15 +02:00
}
/**
2015-01-16 14:23:05 +01:00
* Return true if the given action is for admins
2014-07-20 12:26:15 +02:00
*
* @access public
2015-01-16 14:23:05 +01:00
* @param string $controller Controller name
* @param string $action Action name
2014-07-20 12:26:15 +02:00
* @return bool
*/
2015-01-16 14:23:05 +01:00
public function isAdminAction($controller, $action)
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
return $this->matchAcl($this->admin_acl, $controller, $action);
2014-07-20 12:26:15 +02:00
}
/**
2015-01-16 14:23:05 +01:00
* Return true if the given action is for project managers
2014-07-20 12:26:15 +02:00
*
* @access public
2015-01-16 14:23:05 +01:00
* @param string $controller Controller name
* @param string $action Action name
* @return bool
2014-07-20 12:26:15 +02:00
*/
2015-01-16 14:23:05 +01:00
public function isManagerAction($controller, $action)
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
return $this->matchAcl($this->manager_acl, $controller, $action);
2014-07-20 12:26:15 +02:00
}
/**
2015-01-16 14:23:05 +01:00
* Return true if the given action is for project members
2014-07-20 12:26:15 +02:00
*
* @access public
2015-01-16 14:23:05 +01:00
* @param string $controller Controller name
* @param string $action Action name
2014-07-20 12:26:15 +02:00
* @return bool
*/
2015-01-16 14:23:05 +01:00
public function isMemberAction($controller, $action)
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
return $this->matchAcl($this->member_acl, $controller, $action);
2014-07-20 12:26:15 +02:00
}
/**
2015-01-16 14:23:05 +01:00
* Return true if the visitor is allowed to access to the given page
* We suppose the user already authenticated
2014-07-20 12:26:15 +02:00
*
* @access public
2015-01-16 14:23:05 +01:00
* @param string $controller Controller name
* @param string $action Action name
* @param integer $project_id Project id
2014-07-20 12:26:15 +02:00
* @return bool
*/
2015-01-16 14:23:05 +01:00
public function isAllowed($controller, $action, $project_id = 0)
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
// If you are admin you have access to everything
if ($this->userSession->isAdmin()) {
return true;
2014-07-20 12:26:15 +02:00
}
2015-01-16 14:23:05 +01:00
// If you access to an admin action, your are not allowed
if ($this->isAdminAction($controller, $action)) {
return false;
}
// Check project manager permissions
if ($this->isManagerAction($controller, $action)) {
return $this->isManagerActionAllowed($project_id);
}
// Check project member permissions
if ($this->isMemberAction($controller, $action)) {
return $project_id > 0 && $this->projectPermission->isMember($project_id, $this->userSession->getId());
}
// Other applications actions are allowed
return true;
2014-07-20 12:26:15 +02:00
}
2015-01-16 14:23:05 +01:00
public function isManagerActionAllowed($project_id)
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
if ($this->userSession->isAdmin()) {
return true;
}
return $project_id > 0 && $this->projectPermission->isManager($project_id, $this->userSession->getId());
2014-07-20 12:26:15 +02:00
}
}