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/Controller/User.php

506 lines
14 KiB
PHP
Raw Normal View History

2014-07-20 12:26:15 +02:00
<?php
namespace Controller;
/**
* User controller
*
* @package controller
* @author Frederic Guillot
*/
class User extends Base
{
/**
* Logout and destroy session
*
* @access public
*/
public function logout()
{
$this->checkCSRFParam();
2014-10-22 19:59:09 +02:00
$this->authentication->backend('rememberMe')->destroy($this->acl->getUserId());
2014-07-20 12:26:15 +02:00
$this->session->close();
$this->response->redirect('?controller=user&action=login');
}
/**
* Display the form login
*
* @access public
*/
public function login()
{
2014-10-22 19:59:09 +02:00
if ($this->acl->isLogged()) {
2014-07-20 12:26:15 +02:00
$this->response->redirect('?controller=app');
}
$this->response->html($this->template->layout('user_login', array(
'errors' => array(),
'values' => array(),
'no_layout' => true,
'title' => t('Login')
)));
}
/**
* Check credentials
*
* @access public
*/
public function check()
{
$values = $this->request->getValues();
2014-10-22 19:59:09 +02:00
list($valid, $errors) = $this->authentication->validateForm($values);
2014-07-20 12:26:15 +02:00
if ($valid) {
2014-10-22 19:59:09 +02:00
$this->response->redirect('?controller=board');
2014-07-20 12:26:15 +02:00
}
$this->response->html($this->template->layout('user_login', array(
'errors' => $errors,
'values' => $values,
'no_layout' => true,
'title' => t('Login')
)));
}
2014-10-22 19:59:09 +02:00
/**
* Common layout for project views
*
* @access private
* @param string $template Template name
* @param array $params Template parameters
* @return string
*/
private function layout($template, array $params)
{
$content = $this->template->load($template, $params);
$params['user_content_for_layout'] = $content;
$params['menu'] = 'users';
if (isset($params['user'])) {
$params['title'] = $params['user']['name'] ?: $params['user']['username'];
}
return $this->template->layout('user_layout', $params);
}
/**
* Common method to get the user
*
* @access private
* @return array
*/
private function getUser()
{
$user = $this->user->getById($this->request->getIntegerParam('user_id'));
if (! $user) {
$this->notfound();
}
if ($this->acl->isRegularUser() && $this->acl->getUserId() != $user['id']) {
$this->forbidden();
}
return $user;
}
2014-07-20 12:26:15 +02:00
/**
* List all users
*
* @access public
*/
public function index()
{
$users = $this->user->getAll();
$nb_users = count($users);
$this->response->html(
$this->template->layout('user_index', array(
'projects' => $this->project->getList(),
'users' => $users,
'nb_users' => $nb_users,
'menu' => 'users',
'title' => t('Users').' ('.$nb_users.')'
)));
}
/**
* Display a form to create a new user
*
* @access public
*/
public function create()
{
$this->response->html($this->template->layout('user_new', array(
'projects' => $this->project->getList(),
'errors' => array(),
'values' => array(),
'menu' => 'users',
'title' => t('New user')
)));
}
/**
* Validate and save a new user
*
* @access public
*/
public function save()
{
$values = $this->request->getValues();
list($valid, $errors) = $this->user->validateCreation($values);
if ($valid) {
if ($this->user->create($values)) {
$this->session->flash(t('User created successfully.'));
$this->response->redirect('?controller=user');
}
else {
$this->session->flashError(t('Unable to create your user.'));
}
}
$this->response->html($this->template->layout('user_new', array(
'projects' => $this->project->getList(),
'errors' => $errors,
'values' => $values,
'menu' => 'users',
'title' => t('New user')
)));
}
/**
2014-10-22 19:59:09 +02:00
* Display user information
2014-07-20 12:26:15 +02:00
*
* @access public
*/
2014-10-22 19:59:09 +02:00
public function show()
2014-07-20 12:26:15 +02:00
{
2014-10-22 19:59:09 +02:00
$user = $this->getUser();
$this->response->html($this->layout('user_show', array(
'projects' => $this->project->getAvailableList($user['id']),
'user' => $user,
)));
}
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
/**
* Display last connections
*
* @access public
*/
public function last()
{
$user = $this->getUser();
$this->response->html($this->layout('user_last', array(
'last_logins' => $this->lastLogin->getAll($user['id']),
'user' => $user,
)));
}
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
/**
* Display user sessions
*
* @access public
*/
public function sessions()
{
$user = $this->getUser();
$this->response->html($this->layout('user_sessions', array(
'sessions' => $this->authentication->backend('rememberMe')->getAll($user['id']),
'user' => $user,
)));
}
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
/**
* Remove a "RememberMe" token
*
* @access public
*/
public function removeSession()
{
$this->checkCSRFParam();
$user = $this->getUser();
$this->authentication->backend('rememberMe')->remove($this->request->getIntegerParam('id'));
$this->response->redirect('?controller=user&action=sessions&user_id='.$user['id']);
}
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
/**
* Display user notifications
*
* @access public
*/
public function notifications()
{
$user = $this->getUser();
if ($this->request->isPost()) {
$values = $this->request->getValues();
$this->notification->saveSettings($user['id'], $values);
$this->session->flash(t('User updated successfully.'));
$this->response->redirect('?controller=user&action=notifications&user_id='.$user['id']);
}
$this->response->html($this->layout('user_notifications', array(
'projects' => $this->project->getAvailableList($user['id']),
'notifications' => $this->notification->readSettings($user['id']),
'user' => $user,
2014-07-20 12:26:15 +02:00
)));
}
/**
2014-10-22 19:59:09 +02:00
* Display external accounts
2014-07-20 12:26:15 +02:00
*
* @access public
*/
2014-10-22 19:59:09 +02:00
public function external()
2014-07-20 12:26:15 +02:00
{
2014-10-22 19:59:09 +02:00
$user = $this->getUser();
$this->response->html($this->layout('user_external', array(
'last_logins' => $this->lastLogin->getAll($user['id']),
'user' => $user,
)));
}
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
/**
* Password modification
*
* @access public
*/
public function password()
{
$user = $this->getUser();
$values = array('id' => $user['id']);
$errors = array();
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if ($this->request->isPost()) {
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
$values = $this->request->getValues();
list($valid, $errors) = $this->user->validatePasswordModification($values);
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if ($valid) {
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if ($this->user->update($values)) {
$this->session->flash(t('Password modified successfully.'));
}
else {
$this->session->flashError(t('Unable to change the password.'));
}
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
$this->response->redirect('?controller=user&action=show&user_id='.$user['id']);
2014-07-20 12:26:15 +02:00
}
}
2014-10-22 19:59:09 +02:00
$this->response->html($this->layout('user_password', array(
2014-07-20 12:26:15 +02:00
'values' => $values,
2014-10-22 19:59:09 +02:00
'errors' => $errors,
'user' => $user,
2014-07-20 12:26:15 +02:00
)));
}
/**
2014-10-22 19:59:09 +02:00
* Display a form to edit a user
2014-07-20 12:26:15 +02:00
*
* @access public
*/
2014-10-22 19:59:09 +02:00
public function edit()
2014-07-20 12:26:15 +02:00
{
2014-10-22 19:59:09 +02:00
$user = $this->getUser();
$values = $user;
$errors = array();
unset($values['password']);
if ($this->request->isPost()) {
$values = $this->request->getValues();
if ($this->acl->isAdminUser()) {
$values += array('is_admin' => 0);
}
else {
if (isset($values['is_admin'])) {
unset($values['is_admin']); // Regular users can't be admin
}
}
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
list($valid, $errors) = $this->user->validateModification($values);
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if ($valid) {
if ($this->user->update($values)) {
$this->session->flash(t('User updated successfully.'));
}
else {
$this->session->flashError(t('Unable to update your user.'));
}
$this->response->redirect('?controller=user&action=show&user_id='.$user['id']);
}
}
$this->response->html($this->layout('user_edit', array(
'values' => $values,
'errors' => $errors,
'projects' => $this->project->filterListByAccess($this->project->getList(), $user['id']),
2014-07-20 12:26:15 +02:00
'user' => $user,
)));
}
/**
* Remove a user
*
* @access public
*/
public function remove()
{
2014-10-22 19:59:09 +02:00
$user = $this->getUser();
if ($this->request->getStringParam('confirmation') === 'yes') {
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
$this->checkCSRFParam();
if ($this->user->remove($user['id'])) {
$this->session->flash(t('User removed successfully.'));
} else {
$this->session->flashError(t('Unable to remove this user.'));
}
$this->response->redirect('?controller=user');
2014-07-20 12:26:15 +02:00
}
2014-10-22 19:59:09 +02:00
$this->response->html($this->layout('user_remove', array(
'user' => $user,
)));
2014-07-20 12:26:15 +02:00
}
/**
* Google authentication
*
* @access public
*/
public function google()
{
$code = $this->request->getStringParam('code');
if ($code) {
2014-10-22 19:59:09 +02:00
$profile = $this->authentication->backend('google')->getGoogleProfile($code);
2014-07-20 12:26:15 +02:00
if (is_array($profile)) {
// If the user is already logged, link the account otherwise authenticate
if ($this->acl->isLogged()) {
2014-10-22 19:59:09 +02:00
if ($this->authentication->backend('google')->updateUser($this->acl->getUserId(), $profile)) {
2014-07-20 12:26:15 +02:00
$this->session->flash(t('Your Google Account is linked to your profile successfully.'));
}
else {
$this->session->flashError(t('Unable to link your Google Account.'));
}
2014-10-22 19:59:09 +02:00
$this->response->redirect('?controller=user&action=external&user_id='.$this->acl->getUserId());
2014-07-20 12:26:15 +02:00
}
2014-10-22 19:59:09 +02:00
else if ($this->authentication->backend('google')->authenticate($profile['id'])) {
2014-07-20 12:26:15 +02:00
$this->response->redirect('?controller=app');
}
else {
$this->response->html($this->template->layout('user_login', array(
'errors' => array('login' => t('Google authentication failed')),
'values' => array(),
'no_layout' => true,
'title' => t('Login')
)));
}
}
}
2014-10-22 19:59:09 +02:00
$this->response->redirect($this->authentication->backend('google')->getAuthorizationUrl());
2014-07-20 12:26:15 +02:00
}
/**
* Unlink a Google account
*
* @access public
*/
public function unlinkGoogle()
{
$this->checkCSRFParam();
2014-10-22 19:59:09 +02:00
if ($this->authentication->backend('google')->unlink($this->acl->getUserId())) {
2014-07-20 12:26:15 +02:00
$this->session->flash(t('Your Google Account is not linked anymore to your profile.'));
}
else {
$this->session->flashError(t('Unable to unlink your Google Account.'));
}
2014-10-22 19:59:09 +02:00
$this->response->redirect('?controller=user&action=external&user_id='.$this->acl->getUserId());
2014-07-20 12:26:15 +02:00
}
/**
* GitHub authentication
*
* @access public
*/
public function gitHub()
{
$code = $this->request->getStringParam('code');
if ($code) {
2014-10-22 19:59:09 +02:00
$profile = $this->authentication->backend('gitHub')->getGitHubProfile($code);
2014-07-20 12:26:15 +02:00
if (is_array($profile)) {
// If the user is already logged, link the account otherwise authenticate
if ($this->acl->isLogged()) {
2014-10-22 19:59:09 +02:00
if ($this->authentication->backend('gitHub')->updateUser($this->acl->getUserId(), $profile)) {
2014-07-20 12:26:15 +02:00
$this->session->flash(t('Your GitHub account was successfully linked to your profile.'));
}
else {
$this->session->flashError(t('Unable to link your GitHub Account.'));
}
2014-10-22 19:59:09 +02:00
$this->response->redirect('?controller=user&action=external&user_id='.$this->acl->getUserId());
2014-07-20 12:26:15 +02:00
}
2014-10-22 19:59:09 +02:00
else if ($this->authentication->backend('gitHub')->authenticate($profile['id'])) {
2014-07-20 12:26:15 +02:00
$this->response->redirect('?controller=app');
}
else {
$this->response->html($this->template->layout('user_login', array(
'errors' => array('login' => t('GitHub authentication failed')),
'values' => array(),
'no_layout' => true,
'title' => t('Login')
)));
}
}
}
2014-10-22 19:59:09 +02:00
$this->response->redirect($this->authentication->backend('gitHub')->getAuthorizationUrl());
2014-07-20 12:26:15 +02:00
}
/**
* Unlink a GitHub account
*
* @access public
*/
public function unlinkGitHub()
{
$this->checkCSRFParam();
2014-10-22 19:59:09 +02:00
$this->authentication->backend('gitHub')->revokeGitHubAccess();
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if ($this->authentication->backend('gitHub')->unlink($this->acl->getUserId())) {
2014-07-20 12:26:15 +02:00
$this->session->flash(t('Your GitHub account is no longer linked to your profile.'));
}
else {
$this->session->flashError(t('Unable to unlink your GitHub Account.'));
}
2014-10-22 19:59:09 +02:00
$this->response->redirect('?controller=user&action=external&user_id='.$this->acl->getUserId());
2014-07-20 12:26:15 +02:00
}
}