summaryrefslogtreecommitdiff
path: root/app/Model/ProjectPermission.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Model/ProjectPermission.php')
-rw-r--r--app/Model/ProjectPermission.php251
1 files changed, 209 insertions, 42 deletions
diff --git a/app/Model/ProjectPermission.php b/app/Model/ProjectPermission.php
index fb9847b5..b0a09df4 100644
--- a/app/Model/ProjectPermission.php
+++ b/app/Model/ProjectPermission.php
@@ -27,11 +27,16 @@ class ProjectPermission extends Base
* @param integer $project_id Project id
* @param bool $prepend_unassigned Prepend the 'Unassigned' value
* @param bool $prepend_everybody Prepend the 'Everbody' value
+ * @param bool $allow_single_user If there is only one user return only this user
* @return array
*/
- public function getUsersList($project_id, $prepend_unassigned = true, $prepend_everybody = false)
+ public function getMemberList($project_id, $prepend_unassigned = true, $prepend_everybody = false, $allow_single_user = false)
{
- $allowed_users = $this->getAllowedUsers($project_id);
+ $allowed_users = $this->getMembers($project_id);
+
+ if ($allow_single_user && count($allowed_users) === 1) {
+ return $allowed_users;
+ }
if ($prepend_unassigned) {
$allowed_users = array(t('Unassigned')) + $allowed_users;
@@ -51,7 +56,7 @@ class ProjectPermission extends Base
* @param integer $project_id Project id
* @return array
*/
- public function getAllowedUsers($project_id)
+ public function getMembers($project_id)
{
if ($this->isEverybodyAllowed($project_id)) {
return $this->user->getList();
@@ -81,6 +86,27 @@ class ProjectPermission extends Base
}
/**
+ * Get a list of owners for a project
+ *
+ * @access public
+ * @param integer $project_id Project id
+ * @return array
+ */
+ public function getManagers($project_id)
+ {
+ $users = $this->db
+ ->table(self::TABLE)
+ ->join(User::TABLE, 'id', 'user_id')
+ ->eq('project_id', $project_id)
+ ->eq('is_owner', 1)
+ ->asc('username')
+ ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name')
+ ->findAll();
+
+ return $this->user->prepareList($users);
+ }
+
+ /**
* Get allowed and not allowed users for a project
*
* @access public
@@ -92,11 +118,13 @@ class ProjectPermission extends Base
$users = array(
'allowed' => array(),
'not_allowed' => array(),
+ 'managers' => array(),
);
$all_users = $this->user->getList();
- $users['allowed'] = $this->getAllowedUsers($project_id);
+ $users['allowed'] = $this->getMembers($project_id);
+ $users['managers'] = $this->getManagers($project_id);
foreach ($all_users as $user_id => $username) {
@@ -109,14 +137,14 @@ class ProjectPermission extends Base
}
/**
- * Allow a specific user for a given project
+ * Add a new project member
*
* @access public
* @param integer $project_id Project id
* @param integer $user_id User id
* @return bool
*/
- public function allowUser($project_id, $user_id)
+ public function addMember($project_id, $user_id)
{
return $this->db
->table(self::TABLE)
@@ -124,14 +152,14 @@ class ProjectPermission extends Base
}
/**
- * Revoke a specific user for a given project
+ * Remove a member
*
* @access public
* @param integer $project_id Project id
* @param integer $user_id User id
* @return bool
*/
- public function revokeUser($project_id, $user_id)
+ public function revokeMember($project_id, $user_id)
{
return $this->db
->table(self::TABLE)
@@ -141,61 +169,104 @@ class ProjectPermission extends Base
}
/**
- * Check if a specific user is allowed to access to a given project
+ * Add a project manager
*
* @access public
* @param integer $project_id Project id
* @param integer $user_id User id
* @return bool
*/
- public function isUserAllowed($project_id, $user_id)
+ public function addManager($project_id, $user_id)
{
- if ($this->user->isAdmin($user_id)) {
- return true;
- }
+ return $this->db
+ ->table(self::TABLE)
+ ->save(array('project_id' => $project_id, 'user_id' => $user_id, 'is_owner' => 1));
+ }
+
+ /**
+ * Change the role of a member
+ *
+ * @access public
+ * @param integer $project_id Project id
+ * @param integer $user_id User id
+ * @param integer $is_owner Is user owner of the project
+ * @return bool
+ */
+ public function changeRole($project_id, $user_id, $is_owner)
+ {
+ return $this->db
+ ->table(self::TABLE)
+ ->eq('project_id', $project_id)
+ ->eq('user_id', $user_id)
+ ->update(array('is_owner' => (int) $is_owner));
+ }
+ /**
+ * Check if a specific user is member of a project
+ *
+ * @access public
+ * @param integer $project_id Project id
+ * @param integer $user_id User id
+ * @return bool
+ */
+ public function isMember($project_id, $user_id)
+ {
if ($this->isEverybodyAllowed($project_id)) {
return true;
}
- return (bool) $this->db
+ return $this->db
->table(self::TABLE)
->eq('project_id', $project_id)
->eq('user_id', $user_id)
- ->count();
- }
+ ->count() === 1;
+ }
- /**
- * Return true if everybody is allowed for the project
+ /**
+ * Check if a specific user is manager of a given project
*
* @access public
* @param integer $project_id Project id
+ * @param integer $user_id User id
* @return bool
*/
- public function isEverybodyAllowed($project_id)
+ public function isManager($project_id, $user_id)
{
- return (bool) $this->db
- ->table(Project::TABLE)
- ->eq('id', $project_id)
- ->eq('is_everybody_allowed', 1)
- ->count();
+ return $this->db
+ ->table(self::TABLE)
+ ->eq('project_id', $project_id)
+ ->eq('user_id', $user_id)
+ ->eq('is_owner', 1)
+ ->count() === 1;
}
/**
- * Check if a specific user is allowed to manage a project
+ * Check if a specific user is allowed to access to a given project
*
* @access public
* @param integer $project_id Project id
* @param integer $user_id User id
* @return bool
*/
- public function adminAllowed($project_id, $user_id)
+ public function isUserAllowed($project_id, $user_id)
{
- if ($this->isUserAllowed($project_id, $user_id) && $this->project->isPrivate($project_id)) {
- return true;
- }
+ return $project_id === 0 || $this->user->isAdmin($user_id) || $this->isMember($project_id, $user_id);
+ }
- return false;
+ /**
+ * Return true if everybody is allowed for the project
+ *
+ * @access public
+ * @param integer $project_id Project id
+ * @return bool
+ */
+ public function isEverybodyAllowed($project_id)
+ {
+ return $this->db
+ ->table(Project::TABLE)
+ ->eq('id', $project_id)
+ ->eq('is_everybody_allowed', 1)
+ ->count() === 1;
}
/**
@@ -204,12 +275,13 @@ class ProjectPermission extends Base
* @access public
* @param array $projects Project list: ['project_id' => 'project_name']
* @param integer $user_id User id
+ * @param string $filter Method name to apply
* @return array
*/
- public function filterProjects(array $projects, $user_id)
+ public function filterProjects(array $projects, $user_id, $filter = 'isUserAllowed')
{
foreach ($projects as $project_id => $project_name) {
- if (! $this->isUserAllowed($project_id, $user_id)) {
+ if (! $this->$filter($project_id, $user_id)) {
unset($projects[$project_id]);
}
}
@@ -218,7 +290,7 @@ class ProjectPermission extends Base
}
/**
- * Return a list of projects for a given user
+ * Return a list of allowed active projects for a given user
*
* @access public
* @param integer $user_id User id
@@ -226,23 +298,117 @@ class ProjectPermission extends Base
*/
public function getAllowedProjects($user_id)
{
- return $this->filterProjects($this->project->getListByStatus(Project::ACTIVE), $user_id);
+ if ($this->user->isAdmin($user_id)) {
+ return $this->project->getListByStatus(Project::ACTIVE);
+ }
+
+ return $this->getActiveMemberProjects($user_id);
+ }
+
+ /**
+ * Return a list of projects where the user is member
+ *
+ * @access public
+ * @param integer $user_id User id
+ * @return array
+ */
+ public function getMemberProjects($user_id)
+ {
+ return $this->db
+ ->hashtable(Project::TABLE)
+ ->beginOr()
+ ->eq(self::TABLE.'.user_id', $user_id)
+ ->eq(Project::TABLE.'.is_everybody_allowed', 1)
+ ->closeOr()
+ ->join(self::TABLE, 'project_id', 'id')
+ ->getAll('projects.id', 'name');
+ }
+
+ /**
+ * Return a list of project ids where the user is member
+ *
+ * @access public
+ * @param integer $user_id User id
+ * @return array
+ */
+ public function getMemberProjectIds($user_id)
+ {
+ return $this->db
+ ->table(Project::TABLE)
+ ->beginOr()
+ ->eq(self::TABLE.'.user_id', $user_id)
+ ->eq(Project::TABLE.'.is_everybody_allowed', 1)
+ ->closeOr()
+ ->join(self::TABLE, 'project_id', 'id')
+ ->findAllByColumn('projects.id');
+ }
+
+ /**
+ * Return a list of active project ids where the user is member
+ *
+ * @access public
+ * @param integer $user_id User id
+ * @return array
+ */
+ public function getActiveMemberProjectIds($user_id)
+ {
+ return $this->db
+ ->table(Project::TABLE)
+ ->beginOr()
+ ->eq(self::TABLE.'.user_id', $user_id)
+ ->eq(Project::TABLE.'.is_everybody_allowed', 1)
+ ->closeOr()
+ ->eq(Project::TABLE.'.is_active', Project::ACTIVE)
+ ->join(self::TABLE, 'project_id', 'id')
+ ->findAllByColumn('projects.id');
+ }
+
+ /**
+ * Return a list of active projects where the user is member
+ *
+ * @access public
+ * @param integer $user_id User id
+ * @return array
+ */
+ public function getActiveMemberProjects($user_id)
+ {
+ return $this->db
+ ->hashtable(Project::TABLE)
+ ->beginOr()
+ ->eq(self::TABLE.'.user_id', $user_id)
+ ->eq(Project::TABLE.'.is_everybody_allowed', 1)
+ ->closeOr()
+ ->eq(Project::TABLE.'.is_active', Project::ACTIVE)
+ ->join(self::TABLE, 'project_id', 'id')
+ ->getAll('projects.id', 'name');
}
/**
* Copy user access from a project to another one
*
- * @author Antonio Rabelo
- * @param integer $project_from Project Template
- * @return integer $project_to Project that receives the copy
+ * @param integer $project_src Project Template
+ * @return integer $project_dst Project that receives the copy
* @return boolean
*/
- public function duplicate($project_from, $project_to)
+ public function duplicate($project_src, $project_dst)
{
- $users = $this->getAllowedUsers($project_from);
-
- foreach ($users as $user_id => $name) {
- if (! $this->allowUser($project_to, $user_id)) {
+ $rows = $this->db
+ ->table(self::TABLE)
+ ->columns('project_id', 'user_id', 'is_owner')
+ ->eq('project_id', $project_src)
+ ->findAll();
+
+ foreach ($rows as $row) {
+
+ $result = $this->db
+ ->table(self::TABLE)
+ ->save(array(
+ 'project_id' => $project_dst,
+ 'user_id' => $row['user_id'],
+ 'is_owner' => (int) $row['is_owner'], // (int) for postgres
+ ));
+
+ if (! $result) {
return false;
}
}
@@ -264,6 +430,7 @@ class ProjectPermission extends Base
new Validators\Integer('project_id', t('This value must be an integer')),
new Validators\Required('user_id', t('The user id is required')),
new Validators\Integer('user_id', t('This value must be an integer')),
+ new Validators\Integer('is_owner', t('This value must be an integer')),
));
return array(