Add ical export for users

This commit is contained in:
Frederic Guillot 2015-05-18 12:56:32 -04:00
parent ac6e7bdfbf
commit 46eafe105f
11 changed files with 231 additions and 11 deletions

View File

@ -2,7 +2,9 @@
namespace Controller;
use Model\TaskFilter;
use Model\Task as TaskModel;
use Eluceo\iCal\Component\Calendar as iCalendar;
/**
* iCalendar controller
@ -12,6 +14,35 @@ use Model\Task as TaskModel;
*/
class Ical extends Base
{
/**
* Get user iCalendar
*
* @access public
*/
public function user()
{
$token = $this->request->getStringParam('token');
$user = $this->user->getByToken($token);
// Token verification
if (empty($user)) {
$this->forbidden(true);
}
// Common filter
$filter = $this->taskFilter
->create()
->filterByOwner($user['id']);
// Calendar properties
$calendar = new iCalendar('Kanboard');
$calendar->setName($user['name'] ?: $user['username']);
$calendar->setDescription($user['name'] ?: $user['username']);
$calendar->setPublishedTTL('PT1H');
$this->renderCalendar($filter, $calendar);
}
/**
* Get project iCalendar
*
@ -27,24 +58,40 @@ class Ical extends Base
$this->forbidden(true);
}
$start = $this->request->getStringParam('start', strtotime('-1 month'));
$end = $this->request->getStringParam('end', strtotime('+2 months'));
// Common filter
$filter = $this->taskFilter
->create()
->filterByProject($project['id']);
// Calendar properties
$calendar = new iCalendar('Kanboard');
$calendar->setName($project['name']);
$calendar->setDescription($project['name']);
$calendar->setPublishedTTL('PT1H');
$this->renderCalendar($filter, $calendar);
}
/**
* Common method to render iCal events
*
* @access private
*/
private function renderCalendar(TaskFilter $filter, iCalendar $calendar)
{
$start = $this->request->getStringParam('start', strtotime('-1 month'));
$end = $this->request->getStringParam('end', strtotime('+2 months'));
// Tasks
if ($this->config->get('calendar_project_tasks', 'date_started') === 'date_creation') {
$calendar = $filter->copy()->filterByCreationDateRange($start, $end)->addDateTimeIcalEvents('date_creation', 'date_completed');
$filter->copy()->filterByCreationDateRange($start, $end)->addDateTimeIcalEvents('date_creation', 'date_completed', $calendar);
}
else {
$calendar = $filter->copy()->filterByStartDateRange($start, $end)->addDateTimeIcalEvents('date_started', 'date_completed');
$filter->copy()->filterByStartDateRange($start, $end)->addDateTimeIcalEvents('date_started', 'date_completed', $calendar);
}
// Tasks with due date
$calendar = $filter->copy()->filterByDueDateRange($start, $end)->addAllDayIcalEvents('date_due', $calendar);
$filter->copy()->filterByDueDateRange($start, $end)->addAllDayIcalEvents('date_due', $calendar);
$this->response->contentType('text/calendar; charset=utf-8');
echo $calendar->render();

View File

@ -248,6 +248,35 @@ class User extends Base
)));
}
/**
* Public access management
*
* @access public
*/
public function share()
{
$user = $this->getUser();
$switch = $this->request->getStringParam('switch');
if ($switch === 'enable' || $switch === 'disable') {
$this->checkCSRFParam();
if ($this->user->{$switch.'PublicAccess'}($user['id'])) {
$this->session->flash(t('User updated successfully.'));
} else {
$this->session->flashError(t('Unable to update this user.'));
}
$this->response->redirect($this->helper->url('user', 'share', array('user_id' => $user['id'])));
}
$this->response->html($this->layout('user/share', array(
'user' => $user,
'title' => t('Public access'),
)));
}
/**
* Password modification
*

View File

@ -84,6 +84,10 @@ class Project extends Base
*/
public function getByToken($token)
{
if (empty($token)) {
return false;
}
return $this->db->table(self::TABLE)->eq('token', $token)->eq('is_public', 1)->findOne();
}

View File

@ -5,6 +5,7 @@ namespace Model;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
use Core\Session;
use Core\Security;
/**
* User model
@ -114,6 +115,10 @@ class User extends Base
*/
public function getByGoogleId($google_id)
{
if (empty($google_id)) {
return false;
}
return $this->db->table(self::TABLE)->eq('google_id', $google_id)->findOne();
}
@ -126,6 +131,10 @@ class User extends Base
*/
public function getByGitHubId($github_id)
{
if (empty($github_id)) {
return false;
}
return $this->db->table(self::TABLE)->eq('github_id', $github_id)->findOne();
}
@ -157,6 +166,22 @@ class User extends Base
return $this->db->table(self::TABLE)->eq('email', $email)->findOne();
}
/**
* Fetch user by using the token
*
* @access public
* @param string $token Token
* @return array
*/
public function getByToken($token)
{
if (empty($token)) {
return false;
}
return $this->db->table(self::TABLE)->eq('token', $token)->findOne();
}
/**
* Get all users
*
@ -300,6 +325,36 @@ class User extends Base
});
}
/**
* Enable public access for a user
*
* @access public
* @param integer $user_id User id
* @return bool
*/
public function enablePublicAccess($user_id)
{
return $this->db
->table(self::TABLE)
->eq('id', $user_id)
->save(array('token' => Security::generateToken()));
}
/**
* Disable public access for a user
*
* @access public
* @param integer $user_id User id
* @return bool
*/
public function disablePublicAccess($user_id)
{
return $this->db
->table(self::TABLE)
->eq('id', $user_id)
->save(array('token' => ''));
}
/**
* Common validation rules
*

View File

@ -6,7 +6,12 @@ use PDO;
use Core\Security;
use Model\Link;
const VERSION = 69;
const VERSION = 70;
function version_70($pdo)
{
$pdo->exec("ALTER TABLE users ADD COLUMN token VARCHAR(255) DEFAULT ''");
}
function version_69($pdo)
{

View File

@ -6,7 +6,12 @@ use PDO;
use Core\Security;
use Model\Link;
const VERSION = 50;
const VERSION = 51;
function version_51($pdo)
{
$pdo->exec("ALTER TABLE users ADD COLUMN token VARCHAR(255) DEFAULT ''");
}
function version_50($pdo)
{

View File

@ -6,7 +6,12 @@ use Core\Security;
use PDO;
use Model\Link;
const VERSION = 68;
const VERSION = 69;
function version_69($pdo)
{
$pdo->exec("ALTER TABLE users ADD COLUMN token TEXT DEFAULT ''");
}
function version_68($pdo)
{

View File

@ -0,0 +1,17 @@
<div class="page-header">
<h2><?= t('Public access') ?></h2>
</div>
<?php if (! empty($user['token'])): ?>
<div class="listing">
<ul class="no-bullet">
<li><strong><i class="fa fa-calendar"></i> <?= $this->a(t('iCalendar (iCal format, *.ics)'), 'ical', 'user', array('token' => $user['token']), false, '', '', true) ?></strong></li>
</ul>
</div>
<?= $this->a(t('Disable public access'), 'user', 'share', array('user_id' => $user['id'], 'switch' => 'disable'), true, 'btn btn-red') ?>
<?php else: ?>
<?= $this->a(t('Enable public access'), 'user', 'share', array('user_id' => $user['id'], 'switch' => 'enable'), true, 'btn btn-blue') ?>
<?php endif ?>

View File

@ -5,10 +5,35 @@
<li><?= t('Username:') ?> <strong><?= $this->e($user['username']) ?></strong></li>
<li><?= t('Name:') ?> <strong><?= $this->e($user['name']) ?: t('None') ?></strong></li>
<li><?= t('Email:') ?> <strong><?= $this->e($user['email']) ?: t('None') ?></strong></li>
</ul>
<div class="page-header">
<h2><?= t('Security') ?></h2>
</div>
<ul class="listing">
<li><?= t('Group:') ?> <strong><?= $user['is_admin'] ? t('Administrator') : t('Regular user') ?></strong></li>
<li><?= t('Account type:') ?> <strong><?= $user['is_ldap_user'] ? t('Remote') : t('Local') ?></strong></li>
<li><?= $user['twofactor_activated'] == 1 ? t('Two factor authentication enabled') : t('Two factor authentication disabled') ?></li>
</ul>
<div class="page-header">
<h2><?= t('Preferences') ?></h2>
</div>
<ul class="listing">
<li><?= t('Default project:') ?> <strong><?= (isset($user['default_project_id']) && isset($projects[$user['default_project_id']])) ? $this->e($projects[$user['default_project_id']]) : t('None') ?></strong></li>
<li><?= t('Timezone:') ?> <strong><?= $this->inList($user['timezone'], $timezones) ?></strong></li>
<li><?= t('Language:') ?> <strong><?= $this->inList($user['language'], $languages) ?></strong></li>
<li><?= t('Notifications:') ?> <strong><?= $user['notifications_enabled'] == 1 ? t('Enabled') : t('Disabled') ?></strong></li>
<li><?= t('Group:') ?> <strong><?= $user['is_admin'] ? t('Administrator') : t('Regular user') ?></strong></li>
<li><?= t('Account type:') ?> <strong><?= $user['is_ldap_user'] ? t('Remote') : t('Local') ?></strong></li>
</ul>
<?php if (! empty($user['token'])): ?>
<div class="page-header">
<h2><?= t('Public access') ?></h2>
</div>
<div class="listing">
<ul class="no-bullet">
<li><strong><i class="fa fa-calendar"></i> <?= $this->a(t('iCalendar (iCal format, *.ics)'), 'ical', 'user', array('token' => $user['token']), false, '', '', true) ?></strong></li>
</ul>
</div>
<?php endif ?>

View File

@ -48,6 +48,9 @@
</li>
<?php endif ?>
<li>
<?= $this->a(t('Public access'), 'user', 'share', array('user_id' => $user['id'])) ?>
</li>
<li>
<?= $this->a(t('Email notifications'), 'user', 'notifications', array('user_id' => $user['id'])) ?>
</li>

View File

@ -167,4 +167,29 @@ class UserTest extends Base
$this->assertEmpty($p->getById(2));
}
public function testEnableDisablePublicAccess()
{
$u = new User($this->container);
$this->assertNotFalse($u->create(array('username' => 'toto', 'password' => '123456')));
$user = $u->getById(2);
$this->assertNotEmpty($user);
$this->assertEquals('toto', $user['username']);
$this->assertEmpty($user['token']);
$this->assertTrue($u->enablePublicAccess(2));
$user = $u->getById(2);
$this->assertNotEmpty($user);
$this->assertEquals('toto', $user['username']);
$this->assertNotEmpty($user['token']);
$this->assertTrue($u->disablePublicAccess(2));
$user = $u->getById(2);
$this->assertNotEmpty($user);
$this->assertEquals('toto', $user['username']);
$this->assertEmpty($user['token']);
}
}