|
<?php |
|
|
|
namespace Kanboard\Model; |
|
|
|
use Kanboard\Core\Base; |
|
use Kanboard\Core\Security\Role; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectUserRoleModel extends Base |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
const TABLE = 'project_has_users'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getActiveProjectsByUser($user_id) |
|
{ |
|
return $this->getProjectsByUser($user_id, array(ProjectModel::ACTIVE)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getProjectsByUser($user_id, $status = array(ProjectModel::ACTIVE, ProjectModel::INACTIVE)) |
|
{ |
|
$userProjects = $this->db |
|
->hashtable(ProjectModel::TABLE) |
|
->eq(self::TABLE.'.user_id', $user_id) |
|
->in(ProjectModel::TABLE.'.is_active', $status) |
|
->join(self::TABLE, 'project_id', 'id') |
|
->getAll(ProjectModel::TABLE.'.id', ProjectModel::TABLE.'.name'); |
|
|
|
$groupProjects = $this->projectGroupRoleModel->getProjectsByUser($user_id, $status); |
|
$projects = $userProjects + $groupProjects; |
|
|
|
asort($projects); |
|
|
|
return $projects; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getUserRole($project_id, $user_id) |
|
{ |
|
$role = $this->db->table(self::TABLE)->eq('user_id', $user_id)->eq('project_id', $project_id)->findOneColumn('role'); |
|
|
|
if (empty($role)) { |
|
$role = $this->projectGroupRoleModel->getUserRole($project_id, $user_id); |
|
} |
|
|
|
return $role; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getUsers($project_id) |
|
{ |
|
return $this->db->table(self::TABLE) |
|
->columns( |
|
UserModel::TABLE.'.id', |
|
UserModel::TABLE.'.username', |
|
UserModel::TABLE.'.name', |
|
UserModel::TABLE.'.email', |
|
self::TABLE.'.role' |
|
) |
|
->join(UserModel::TABLE, 'id', 'user_id') |
|
->eq('project_id', $project_id) |
|
->asc(UserModel::TABLE.'.username') |
|
->asc(UserModel::TABLE.'.name') |
|
->findAll(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAllUsers($project_id) |
|
{ |
|
$userMembers = $this->getUsers($project_id); |
|
$groupMembers = $this->projectGroupRoleModel->getUsers($project_id); |
|
$members = array_merge($userMembers, $groupMembers); |
|
|
|
return $this->userModel->prepareList($members); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAllUsersGroupedByRole($project_id) |
|
{ |
|
$users = array(); |
|
|
|
$userMembers = $this->getUsers($project_id); |
|
$groupMembers = $this->projectGroupRoleModel->getUsers($project_id); |
|
$members = array_merge($userMembers, $groupMembers); |
|
|
|
foreach ($members as $user) { |
|
if (! isset($users[$user['role']])) { |
|
$users[$user['role']] = array(); |
|
} |
|
|
|
$users[$user['role']][$user['id']] = $user['name'] ?: $user['username']; |
|
} |
|
|
|
return $users; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAssignableUsers($project_id) |
|
{ |
|
$userMembers = $this->db->table(self::TABLE) |
|
->columns(UserModel::TABLE.'.id', UserModel::TABLE.'.username', UserModel::TABLE.'.name') |
|
->join(UserModel::TABLE, 'id', 'user_id') |
|
->eq(UserModel::TABLE.'.is_active', 1) |
|
->eq(self::TABLE.'.project_id', $project_id) |
|
->neq(self::TABLE.'.role', Role::PROJECT_VIEWER) |
|
->findAll(); |
|
|
|
$groupMembers = $this->projectGroupRoleModel->getAssignableUsers($project_id); |
|
$members = array_merge($userMembers, $groupMembers); |
|
|
|
return $this->userModel->prepareList($members); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAssignableUsersList($project_id, $unassigned = true, $everybody = false, $singleUser = false) |
|
{ |
|
$users = $this->getAssignableUsers($project_id); |
|
|
|
if ($singleUser && count($users) === 1) { |
|
return $users; |
|
} |
|
|
|
if ($unassigned) { |
|
$users = array(t('Unassigned')) + $users; |
|
} |
|
|
|
if ($everybody) { |
|
$users = array(UserModel::EVERYBODY_ID => t('Everybody')) + $users; |
|
} |
|
|
|
return $users; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function addUser($project_id, $user_id, $role) |
|
{ |
|
return $this->db->table(self::TABLE)->insert(array( |
|
'user_id' => $user_id, |
|
'project_id' => $project_id, |
|
'role' => $role, |
|
)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function removeUser($project_id, $user_id) |
|
{ |
|
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->eq('project_id', $project_id)->remove(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function changeUserRole($project_id, $user_id, $role) |
|
{ |
|
return $this->db->table(self::TABLE) |
|
->eq('user_id', $user_id) |
|
->eq('project_id', $project_id) |
|
->update(array( |
|
'role' => $role, |
|
)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function duplicate($project_src_id, $project_dst_id) |
|
{ |
|
$rows = $this->db->table(self::TABLE)->eq('project_id', $project_src_id)->findAll(); |
|
|
|
foreach ($rows as $row) { |
|
$result = $this->db->table(self::TABLE)->save(array( |
|
'project_id' => $project_dst_id, |
|
'user_id' => $row['user_id'], |
|
'role' => $row['role'], |
|
)); |
|
|
|
if (! $result) { |
|
return false; |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
} |
|
|