From e582d4047b061f0c17e6366fed2bf1cabd624c10 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Wed, 25 Nov 2015 22:06:39 -0500 Subject: Add groups (teams) --- app/Controller/Group.php | 255 ++++++++++++++++++++++++++++++++++ app/Model/Group.php | 151 ++++++++++++++++++++ app/Model/GroupMember.php | 95 +++++++++++++ app/Schema/Mysql.php | 24 +++- app/Schema/Postgres.php | 23 ++- app/Schema/Sqlite.php | 37 ++++- app/ServiceProvider/ClassProvider.php | 2 + app/Template/group/associate.php | 25 ++++ app/Template/group/create.php | 19 +++ app/Template/group/dissociate.php | 19 +++ app/Template/group/edit.php | 22 +++ app/Template/group/index.php | 45 ++++++ app/Template/group/remove.php | 19 +++ app/Template/group/users.php | 44 ++++++ app/Template/user/create_local.php | 2 +- app/Template/user/index.php | 1 + 16 files changed, 779 insertions(+), 4 deletions(-) create mode 100644 app/Controller/Group.php create mode 100644 app/Model/Group.php create mode 100644 app/Model/GroupMember.php create mode 100644 app/Template/group/associate.php create mode 100644 app/Template/group/create.php create mode 100644 app/Template/group/dissociate.php create mode 100644 app/Template/group/edit.php create mode 100644 app/Template/group/index.php create mode 100644 app/Template/group/remove.php create mode 100644 app/Template/group/users.php (limited to 'app') diff --git a/app/Controller/Group.php b/app/Controller/Group.php new file mode 100644 index 00000000..4e81f6c1 --- /dev/null +++ b/app/Controller/Group.php @@ -0,0 +1,255 @@ +paginator + ->setUrl('group', 'index') + ->setMax(30) + ->setOrder('name') + ->setQuery($this->group->getQuery()) + ->calculate(); + + $this->response->html($this->template->layout('group/index', array( + 'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()), + 'title' => t('Groups').' ('.$paginator->getTotal().')', + 'paginator' => $paginator, + ))); + } + + /** + * List all users + * + * @access public + */ + public function users() + { + $group_id = $this->request->getIntegerParam('group_id'); + $group = $this->group->getById($group_id); + + $paginator = $this->paginator + ->setUrl('group', 'users') + ->setMax(30) + ->setOrder('username') + ->setQuery($this->groupMember->getQuery($group_id)) + ->calculate(); + + $this->response->html($this->template->layout('group/users', array( + 'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()), + 'title' => t('Members of %s', $group['name']).' ('.$paginator->getTotal().')', + 'paginator' => $paginator, + 'group' => $group, + ))); + } + + /** + * Display a form to create a new group + * + * @access public + */ + public function create(array $values = array(), array $errors = array()) + { + $this->response->html($this->template->layout('group/create', array( + 'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()), + 'errors' => $errors, + 'values' => $values, + 'title' => t('New group') + ))); + } + + /** + * Validate and save a new group + * + * @access public + */ + public function save() + { + $values = $this->request->getValues(); + list($valid, $errors) = $this->group->validateCreation($values); + + if ($valid) { + if ($this->group->create($values['name']) !== false) { + $this->flash->success(t('Group created successfully.')); + $this->response->redirect($this->helper->url->to('group', 'index')); + } else { + $this->flash->failure(t('Unable to create your group.')); + } + } + + $this->create($values, $errors); + } + + /** + * Display a form to update a group + * + * @access public + */ + public function edit(array $values = array(), array $errors = array()) + { + if (empty($values)) { + $values = $this->group->getById($this->request->getIntegerParam('group_id')); + } + + $this->response->html($this->template->layout('group/edit', array( + 'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()), + 'errors' => $errors, + 'values' => $values, + 'title' => t('Edit group') + ))); + } + + /** + * Validate and save a group + * + * @access public + */ + public function update() + { + $values = $this->request->getValues(); + list($valid, $errors) = $this->group->validateModification($values); + + if ($valid) { + if ($this->group->update($values) !== false) { + $this->flash->success(t('Group updated successfully.')); + $this->response->redirect($this->helper->url->to('group', 'index')); + } else { + $this->flash->failure(t('Unable to update your group.')); + } + } + + $this->edit($values, $errors); + } + + /** + * Form to associate a user to a group + * + * @access public + */ + public function associate(array $values = array(), array $errors = array()) + { + $group_id = $this->request->getIntegerParam('group_id'); + $group = $this->group->getbyId($group_id); + + if (empty($values)) { + $values['group_id'] = $group_id; + } + + $this->response->html($this->template->layout('group/associate', array( + 'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()), + 'users' => $this->user->prepareList($this->groupMember->getNotMembers($group_id)), + 'group' => $group, + 'errors' => $errors, + 'values' => $values, + 'title' => t('Add group member to "%s"', $group['name']), + ))); + } + + /** + * Add user to a group + * + * @access public + */ + public function addUser() + { + $values = $this->request->getValues(); + + if (isset($values['group_id']) && isset($values['user_id'])) { + if ($this->groupMember->addUser($values['group_id'], $values['user_id'])) { + $this->flash->success(t('Group member added successfully.')); + $this->response->redirect($this->helper->url->to('group', 'users', array('group_id' => $values['group_id']))); + } else { + $this->flash->failure(t('Unable to add group member.')); + } + } + + $this->associate($values); + } + + /** + * Confirmation dialog to remove a user from a group + * + * @access public + */ + public function dissociate() + { + $group_id = $this->request->getIntegerParam('group_id'); + $user_id = $this->request->getIntegerParam('user_id'); + $group = $this->group->getById($group_id); + $user = $this->user->getById($user_id); + + $this->response->html($this->template->layout('group/dissociate', array( + 'group' => $group, + 'user' => $user, + 'title' => t('Remove a user from group "%s', $group['name']), + ))); + } + + /** + * Remove a user from a group + * + * @access public + */ + public function removeUser() + { + $this->checkCSRFParam(); + $group_id = $this->request->getIntegerParam('group_id'); + $user_id = $this->request->getIntegerParam('user_id'); + + if ($this->groupMember->removeUser($group_id, $user_id)) { + $this->flash->success(t('User removed successfully from this group.')); + } else { + $this->flash->failure(t('Unable to remove this user from the group.')); + } + + $this->response->redirect($this->helper->url->to('group', 'users', array('group_id' => $group_id))); + } + + /** + * Confirmation dialog to remove a group + * + * @access public + */ + public function confirm() + { + $group_id = $this->request->getIntegerParam('group_id'); + $group = $this->group->getById($group_id); + + $this->response->html($this->template->layout('group/remove', array( + 'group' => $group, + 'title' => t('Remove group'), + ))); + } + + /** + * Remove a group + * + * @access public + */ + public function remove() + { + $this->checkCSRFParam(); + $group_id = $this->request->getIntegerParam('group_id'); + + if ($this->group->remove($group_id)) { + $this->flash->success(t('Group removed successfully.')); + } else { + $this->flash->failure(t('Unable to remove this group.')); + } + + $this->response->redirect($this->helper->url->to('group', 'index')); + } +} diff --git a/app/Model/Group.php b/app/Model/Group.php new file mode 100644 index 00000000..82a8887b --- /dev/null +++ b/app/Model/Group.php @@ -0,0 +1,151 @@ +db->table(self::TABLE); + } + + /** + * Get a specific group by id + * + * @access public + * @param integer $group_id + * @return array + */ + public function getById($group_id) + { + return $this->getQuery()->eq('id', $group_id)->findOne(); + } + + /** + * Get all groups + * + * @access public + * @return array + */ + public function getAll() + { + return $this->getQuery()->asc('name')->findAll(); + } + + /** + * Remove a group + * + * @access public + * @param integer $group_id + * @return array + */ + public function remove($group_id) + { + return $this->db->table(self::TABLE)->eq('id', $group_id)->remove(); + } + + /** + * Create a new group + * + * @access public + * @param string $name + * @param string $external_id + * @return integer|boolean + */ + public function create($name, $external_id = '') + { + return $this->persist(self::TABLE, array( + 'name' => $name, + 'external_id' => $external_id, + )); + } + + /** + * Update existing group + * + * @access public + * @param array $values + * @return boolean + */ + public function update(array $values) + { + return $this->db->table(self::TABLE)->eq('id', $values['id'])->update($values); + } + + /** + * Validate creation + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $v = new Validator($values, $this->commonValidationRules()); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Validate modification + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateModification(array $values) + { + $rules = array( + new Validators\Required('id', t('The id is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); + + return array( + $v->execute(), + $v->getErrors() + ); + } + + /** + * Common validation rules + * + * @access private + * @return array + */ + private function commonValidationRules() + { + return array( + new Validators\Required('name', t('The name is required')), + new Validators\MaxLength('name', t('The maximum length is %d characters', 100), 100), + new Validators\Unique('name', t('The name must be unique'), $this->db->getConnection(), self::TABLE, 'id'), + new Validators\MaxLength('external_id', t('The maximum length is %d characters', 255), 255), + new Validators\Integer('id', t('This value must be an integer')), + ); + } +} diff --git a/app/Model/GroupMember.php b/app/Model/GroupMember.php new file mode 100644 index 00000000..04e9d495 --- /dev/null +++ b/app/Model/GroupMember.php @@ -0,0 +1,95 @@ +db->table(self::TABLE) + ->join(User::TABLE, 'id', 'user_id') + ->eq('group_id', $group_id); + } + + /** + * Get all users + * + * @access public + * @param integer $group_id + * @return array + */ + public function getMembers($group_id) + { + return $this->getQuery($group_id)->findAll(); + } + + /** + * Get all not members + * + * @access public + * @param integer $group_id + * @return array + */ + public function getNotMembers($group_id) + { + $subquery = $this->db->table(self::TABLE) + ->columns('user_id') + ->eq('group_id', $group_id); + + return $this->db->table(User::TABLE) + ->notInSubquery('id', $subquery) + ->findAll(); + } + + /** + * Add user to a group + * + * @access public + * @param integer $group_id + * @param integer $user_id + * @return boolean + */ + public function addUser($group_id, $user_id) + { + return $this->db->table(self::TABLE)->insert(array( + 'group_id' => $group_id, + 'user_id' => $user_id, + )); + } + + /** + * Remove user from a group + * + * @access public + * @param integer $group_id + * @param integer $user_id + * @return boolean + */ + public function removeUser($group_id, $user_id) + { + return $this->db->table(self::TABLE) + ->eq('group_id', $group_id) + ->eq('user_id', $user_id) + ->remove(); + } +} diff --git a/app/Schema/Mysql.php b/app/Schema/Mysql.php index 52a73fb1..5a451c77 100644 --- a/app/Schema/Mysql.php +++ b/app/Schema/Mysql.php @@ -5,7 +5,29 @@ namespace Schema; use PDO; use Kanboard\Core\Security\Token; -const VERSION = 94; +const VERSION = 95; + +function version_95(PDO $pdo) +{ + $pdo->exec(" + CREATE TABLE groups ( + id INT NOT NULL AUTO_INCREMENT, + external_id VARCHAR(255) DEFAULT '', + name VARCHAR(100) NOT NULL UNIQUE, + PRIMARY KEY(id) + ) ENGINE=InnoDB CHARSET=utf8 + "); + + $pdo->exec(" + CREATE TABLE group_has_users ( + group_id INT NOT NULL, + user_id INT NOT NULL, + FOREIGN KEY(group_id) REFERENCES groups(id) ON DELETE CASCADE, + FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + UNIQUE(group_id, user_id) + ) ENGINE=InnoDB CHARSET=utf8 + "); +} function version_94(PDO $pdo) { diff --git a/app/Schema/Postgres.php b/app/Schema/Postgres.php index 5cd1a7d0..a3887cfb 100644 --- a/app/Schema/Postgres.php +++ b/app/Schema/Postgres.php @@ -5,7 +5,28 @@ namespace Schema; use PDO; use Kanboard\Core\Security\Token; -const VERSION = 74; +const VERSION = 75; + +function version_75(PDO $pdo) +{ + $pdo->exec(" + CREATE TABLE groups ( + id SERIAL PRIMARY KEY, + external_id VARCHAR(255) DEFAULT '', + name VARCHAR(100) NOT NULL UNIQUE + ) + "); + + $pdo->exec(" + CREATE TABLE group_has_users ( + group_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + FOREIGN KEY(group_id) REFERENCES groups(id) ON DELETE CASCADE, + FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + UNIQUE(group_id, user_id) + ) + "); +} function version_74(PDO $pdo) { diff --git a/app/Schema/Sqlite.php b/app/Schema/Sqlite.php index fa26b158..f0510cff 100644 --- a/app/Schema/Sqlite.php +++ b/app/Schema/Sqlite.php @@ -5,7 +5,42 @@ namespace Schema; use Kanboard\Core\Security\Token; use PDO; -const VERSION = 88; +const VERSION = 89; + +function version_90(PDO $pdo) +{ + $pdo->exec(" + CREATE TABLE project_has_groups ( + group_id INTEGER NOT NULL, + project_id INTEGER NOT NULL, + role TEXT NOT NULL, + FOREIGN KEY(group_id) REFERENCES groups(id) ON DELETE CASCADE, + FOREIGN KEY(project_id) REFERENCES projects(id) ON DELETE CASCADE, + UNIQUE(group_id, project_id) + ) + "); +} + +function version_89(PDO $pdo) +{ + $pdo->exec(" + CREATE TABLE groups ( + id INTEGER PRIMARY KEY, + external_id TEXT DEFAULT '', + name TEXT NOCASE NOT NULL UNIQUE + ) + "); + + $pdo->exec(" + CREATE TABLE group_has_users ( + group_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + FOREIGN KEY(group_id) REFERENCES groups(id) ON DELETE CASCADE, + FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + UNIQUE(group_id, user_id) + ) + "); +} function version_88(PDO $pdo) { diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index 9c9bc233..9ec81116 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -32,6 +32,8 @@ class ClassProvider implements ServiceProviderInterface 'Currency', 'CustomFilter', 'File', + 'Group', + 'GroupMember', 'LastLogin', 'Link', 'Notification', diff --git a/app/Template/group/associate.php b/app/Template/group/associate.php new file mode 100644 index 00000000..dc665bb3 --- /dev/null +++ b/app/Template/group/associate.php @@ -0,0 +1,25 @@ +
+ + +

+ +
+ form->csrf() ?> + form->hidden('group_id', $values) ?> + + form->label(t('User'), 'user_id') ?> + form->select('user_id', $users, $values, $errors, array('required'), 'chosen-select') ?>
+ +
+ + + url->link(t('cancel'), 'group', 'index') ?> +
+
+ +
diff --git a/app/Template/group/create.php b/app/Template/group/create.php new file mode 100644 index 00000000..696e5013 --- /dev/null +++ b/app/Template/group/create.php @@ -0,0 +1,19 @@ +
+ +
+ form->csrf() ?> + + form->label(t('Name'), 'name') ?> + form->text('name', $values, $errors, array('autofocus', 'required', 'maxlength="100"')) ?>
+ +
+ + + url->link(t('cancel'), 'group', 'index') ?> +
+
+
diff --git a/app/Template/group/dissociate.php b/app/Template/group/dissociate.php new file mode 100644 index 00000000..2b0b1af4 --- /dev/null +++ b/app/Template/group/dissociate.php @@ -0,0 +1,19 @@ +
+ +
+

+ +
+ url->link(t('Yes'), 'group', 'removeUser', array('group_id' => $group['id'], 'user_id' => $user['id']), true, 'btn btn-red') ?> + + url->link(t('cancel'), 'group', 'users', array('group_id' => $group['id'])) ?> +
+
+
diff --git a/app/Template/group/edit.php b/app/Template/group/edit.php new file mode 100644 index 00000000..4d7e5e81 --- /dev/null +++ b/app/Template/group/edit.php @@ -0,0 +1,22 @@ +
+ +
+ form->csrf() ?> + + form->hidden('id', $values) ?> + form->hidden('external_id', $values) ?> + + form->label(t('Name'), 'name') ?> + form->text('name', $values, $errors, array('autofocus', 'required', 'maxlength="100"')) ?>
+ +
+ + + url->link(t('cancel'), 'group', 'index') ?> +
+
+
diff --git a/app/Template/group/index.php b/app/Template/group/index.php new file mode 100644 index 00000000..24de02a0 --- /dev/null +++ b/app/Template/group/index.php @@ -0,0 +1,45 @@ +
+ + isEmpty()): ?> +

+ + + + + + + + + getCollection() as $group): ?> + + + + + + + +
order(t('Id'), 'id') ?>order(t('External Id'), 'external_id') ?>order(t('Name'), 'name') ?>
+ # + + e($group['external_id']) ?> + + e($group['name']) ?> + +
    +
  • url->link(t('Add group member'), 'group', 'associate', array('group_id' => $group['id'])) ?>
  • +
  • url->link(t('Users'), 'group', 'users', array('group_id' => $group['id'])) ?>
  • +
  • url->link(t('Edit'), 'group', 'edit', array('group_id' => $group['id'])) ?>
  • +
  • url->link(t('Remove'), 'group', 'confirm', array('group_id' => $group['id'])) ?>
  • +
+
+ + + +
diff --git a/app/Template/group/remove.php b/app/Template/group/remove.php new file mode 100644 index 00000000..48da91d5 --- /dev/null +++ b/app/Template/group/remove.php @@ -0,0 +1,19 @@ +
+ +
+

+ +
+ url->link(t('Yes'), 'group', 'remove', array('group_id' => $group['id']), true, 'btn btn-red') ?> + + url->link(t('cancel'), 'group', 'index') ?> +
+
+
diff --git a/app/Template/group/users.php b/app/Template/group/users.php new file mode 100644 index 00000000..56ad82cf --- /dev/null +++ b/app/Template/group/users.php @@ -0,0 +1,44 @@ +
+ + isEmpty()): ?> +

+ + + + + + + + + + getCollection() as $user): ?> + + + + + + + + +
order(t('Id'), 'id') ?>order(t('Username'), 'username') ?>order(t('Name'), 'name') ?>order(t('Email'), 'email') ?>
+ url->link('#'.$user['id'], 'user', 'show', array('user_id' => $user['id'])) ?> + + url->link($this->e($user['username']), 'user', 'show', array('user_id' => $user['id'])) ?> + + e($user['name']) ?> + + e($user['email']) ?> + + url->link(t('Remove this user'), 'group', 'dissociate', array('group_id' => $group['id'], 'user_id' => $user['id'])) ?> +
+ + + +
diff --git a/app/Template/user/create_local.php b/app/Template/user/create_local.php index 98c38f0d..6e6ca6ac 100644 --- a/app/Template/user/create_local.php +++ b/app/Template/user/create_local.php @@ -49,4 +49,4 @@ - \ No newline at end of file + diff --git a/app/Template/user/index.php b/app/Template/user/index.php index 4008b920..7c6ecc1e 100644 --- a/app/Template/user/index.php +++ b/app/Template/user/index.php @@ -5,6 +5,7 @@
  • url->link(t('New local user'), 'user', 'create') ?>
  • url->link(t('New remote user'), 'user', 'create', array('remote' => 1)) ?>
  • url->link(t('Import'), 'userImport', 'step1') ?>
  • +
  • url->link(t('View all groups'), 'group', 'index') ?>
  • -- cgit v1.2.3