Exclude current user in email notifications, pull-request #273)
This commit is contained in:
@@ -19,6 +19,18 @@ class Session
|
|||||||
*/
|
*/
|
||||||
const SESSION_LIFETIME = 0; // Until the browser is closed
|
const SESSION_LIFETIME = 0; // Until the browser is closed
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the session is open
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @access public
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static function isOpen()
|
||||||
|
{
|
||||||
|
return session_id() !== '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a session
|
* Open a session
|
||||||
*
|
*
|
||||||
@@ -50,14 +62,14 @@ class Session
|
|||||||
ini_set('session.hash_bits_per_character', 6);
|
ini_set('session.hash_bits_per_character', 6);
|
||||||
|
|
||||||
// If session was autostarted with session.auto_start = 1 in php.ini destroy it, otherwise we cannot login
|
// If session was autostarted with session.auto_start = 1 in php.ini destroy it, otherwise we cannot login
|
||||||
if (isset($_SESSION))
|
if (isset($_SESSION)) {
|
||||||
{
|
|
||||||
session_destroy();
|
session_destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Custom session name
|
// Custom session name
|
||||||
session_name('__S');
|
session_name('__S');
|
||||||
|
|
||||||
|
// Start the session
|
||||||
session_start();
|
session_start();
|
||||||
|
|
||||||
// Regenerate the session id to avoid session fixation issue
|
// Regenerate the session id to avoid session fixation issue
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Model;
|
namespace Model;
|
||||||
|
|
||||||
|
use Core\Session;
|
||||||
use Core\Translator;
|
use Core\Translator;
|
||||||
use Core\Template;
|
use Core\Template;
|
||||||
use Event\TaskNotificationListener;
|
use Event\TaskNotificationListener;
|
||||||
@@ -30,15 +31,22 @@ class Notification extends Base
|
|||||||
* Get the list of users to send the notification for a given project
|
* Get the list of users to send the notification for a given project
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $project_id Project id
|
* @param integer $project_id Project id
|
||||||
|
* @param array $exlude_users List of user_id to exclude
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getUsersList($project_id)
|
public function getUsersList($project_id, array $exclude_users = array())
|
||||||
{
|
{
|
||||||
|
// Exclude the connected user
|
||||||
|
if (Session::isOpen()) {
|
||||||
|
$exclude_users[] = $this->acl->getUserId();
|
||||||
|
}
|
||||||
|
|
||||||
$users = $this->db->table(User::TABLE)
|
$users = $this->db->table(User::TABLE)
|
||||||
->columns('id', 'username', 'name', 'email')
|
->columns('id', 'username', 'name', 'email')
|
||||||
->eq('notifications_enabled', '1')
|
->eq('notifications_enabled', '1')
|
||||||
->neq('email', '')
|
->neq('email', '')
|
||||||
|
->notin('id', $exclude_users)
|
||||||
->findAll();
|
->findAll();
|
||||||
|
|
||||||
foreach ($users as $index => $user) {
|
foreach ($users as $index => $user) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Model;
|
|||||||
|
|
||||||
use SimpleValidator\Validator;
|
use SimpleValidator\Validator;
|
||||||
use SimpleValidator\Validators;
|
use SimpleValidator\Validators;
|
||||||
|
use Core\Session;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User model
|
* User model
|
||||||
@@ -205,7 +206,7 @@ class User extends Base
|
|||||||
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->update($values);
|
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->update($values);
|
||||||
|
|
||||||
// If the user is connected refresh his session
|
// If the user is connected refresh his session
|
||||||
if (session_id() !== '' && $_SESSION['user']['id'] == $values['id']) {
|
if (Session::isOpen() && $_SESSION['user']['id'] == $values['id']) {
|
||||||
$this->updateSession();
|
$this->updateSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
67
tests/units/NotificationTest.php
Normal file
67
tests/units/NotificationTest.php
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__.'/Base.php';
|
||||||
|
|
||||||
|
use Model\User;
|
||||||
|
use Model\Project;
|
||||||
|
use Model\Notification;
|
||||||
|
|
||||||
|
class NotificationTest extends Base
|
||||||
|
{
|
||||||
|
public function testGetUserList()
|
||||||
|
{
|
||||||
|
$u = new User($this->registry);
|
||||||
|
$p = new Project($this->registry);
|
||||||
|
$n = new Notification($this->registry);
|
||||||
|
|
||||||
|
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
|
||||||
|
$this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
|
||||||
|
|
||||||
|
// Email + Notifications enabled
|
||||||
|
$this->assertTrue($u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
|
||||||
|
|
||||||
|
// No email + Notifications enabled
|
||||||
|
$this->assertTrue($u->create(array('username' => 'user2', 'email' => '', 'notifications_enabled' => 1)));
|
||||||
|
|
||||||
|
// Email + Notifications enabled
|
||||||
|
$this->assertTrue($u->create(array('username' => 'user3', 'email' => 'user3@here', 'notifications_enabled' => 1)));
|
||||||
|
|
||||||
|
// No email + notifications disabled
|
||||||
|
$this->assertTrue($u->create(array('username' => 'user4')));
|
||||||
|
|
||||||
|
$users = $n->getUsersList(1);
|
||||||
|
$this->assertNotEmpty($users);
|
||||||
|
$this->assertEquals(2, count($users));
|
||||||
|
$this->assertEquals('user1@here', $users[0]['email']);
|
||||||
|
$this->assertEquals('user3@here', $users[1]['email']);
|
||||||
|
|
||||||
|
$users = $n->getUsersList(2);
|
||||||
|
$this->assertNotEmpty($users);
|
||||||
|
$this->assertEquals(2, count($users));
|
||||||
|
$this->assertEquals('user1@here', $users[0]['email']);
|
||||||
|
$this->assertEquals('user3@here', $users[1]['email']);
|
||||||
|
|
||||||
|
// User 3 choose to receive notification only for project 2
|
||||||
|
$n->saveSettings(4, array('notifications_enabled' => 1, 'projects' => array(2 => true)));
|
||||||
|
|
||||||
|
$users = $n->getUsersList(1);
|
||||||
|
$this->assertNotEmpty($users);
|
||||||
|
$this->assertEquals(1, count($users));
|
||||||
|
$this->assertEquals('user1@here', $users[0]['email']);
|
||||||
|
|
||||||
|
$users = $n->getUsersList(2);
|
||||||
|
$this->assertNotEmpty($users);
|
||||||
|
$this->assertEquals(2, count($users));
|
||||||
|
$this->assertEquals('user1@here', $users[0]['email']);
|
||||||
|
$this->assertEquals('user3@here', $users[1]['email']);
|
||||||
|
|
||||||
|
// User 1 excluded
|
||||||
|
$users = $n->getUsersList(1, array(2));
|
||||||
|
$this->assertEmpty($users);
|
||||||
|
|
||||||
|
$users = $n->getUsersList(2, array(2));
|
||||||
|
$this->assertNotEmpty($users);
|
||||||
|
$this->assertEquals(1, count($users));
|
||||||
|
$this->assertEquals('user3@here', $users[0]['email']);
|
||||||
|
}
|
||||||
|
}
|
||||||
4
vendor/PicoDb/Table.php
vendored
4
vendor/PicoDb/Table.php
vendored
@@ -350,7 +350,7 @@ class Table
|
|||||||
switch (strtolower($name)) {
|
switch (strtolower($name)) {
|
||||||
|
|
||||||
case 'in':
|
case 'in':
|
||||||
if (isset($arguments[1]) && is_array($arguments[1])) {
|
if (isset($arguments[1]) && is_array($arguments[1]) && ! empty($arguments[1])) {
|
||||||
|
|
||||||
$sql = sprintf(
|
$sql = sprintf(
|
||||||
'%s IN (%s)',
|
'%s IN (%s)',
|
||||||
@@ -361,7 +361,7 @@ class Table
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'notin':
|
case 'notin':
|
||||||
if (isset($arguments[1]) && is_array($arguments[1])) {
|
if (isset($arguments[1]) && is_array($arguments[1]) && ! empty($arguments[1])) {
|
||||||
|
|
||||||
$sql = sprintf(
|
$sql = sprintf(
|
||||||
'%s NOT IN (%s)',
|
'%s NOT IN (%s)',
|
||||||
|
|||||||
Reference in New Issue
Block a user