Skip to content

Commit

Permalink
[event] adding experimental support for user events
Browse files Browse the repository at this point in the history
  • Loading branch information
Pepijn Over committed Feb 27, 2015
1 parent 1123603 commit 8505a34
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 14 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"phpmailer/phpmailer": "5.2.6",
"symfony/config": "2.6.*",
"symfony/dependency-injection": "2.6.*",
"symfony/event-dispatcher": "2.6.*",
"symfony/http-foundation": "2.6.*",
"php-pushover/php-pushover": "dev-master",
"twig/twig": "1.*"
Expand Down
60 changes: 59 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/sc
<parameters>
<parameter key="config.theme" type="constant">PSM_THEME</parameter>

<parameter key="modules" type="collection">
<parameter>module.config</parameter>
<parameter>module.error</parameter>
<parameter>module.server</parameter>
<parameter>module.user</parameter>
<parameter>module.install</parameter>
</parameter>

<parameter key="path.src" type="constant">PSM_PATH_SRC</parameter>
<parameter key="path.templates">%path.src%templates</parameter>

Expand Down
7 changes: 7 additions & 0 deletions src/psm/Module/Config/ConfigModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@
**/

namespace psm\Module\Config;

use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ConfigModule implements ModuleInterface {

public function load(ContainerBuilder $container) {

}

public function getControllers() {
return array(
'config' => __NAMESPACE__ . '\Controller\ConfigController',
Expand Down
7 changes: 7 additions & 0 deletions src/psm/Module/Error/ErrorModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@
**/

namespace psm\Module\Error;

use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ErrorModule implements ModuleInterface {

public function load(ContainerBuilder $container) {

}

public function getControllers() {
return array(
'error' => __NAMESPACE__ . '\Controller\ErrorController',
Expand Down
7 changes: 7 additions & 0 deletions src/psm/Module/Install/InstallModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@
**/

namespace psm\Module\Install;

use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class InstallModule implements ModuleInterface {

public function load(ContainerBuilder $container) {

}

public function getControllers() {
return array(
'install' => __NAMESPACE__ . '\Controller\InstallController',
Expand Down
5 changes: 4 additions & 1 deletion src/psm/Module/ModuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
**/

namespace psm\Module;
use Symfony\Component\DependencyInjection\ContainerBuilder;

interface ModuleInterface {

public function load(ContainerBuilder $container);

public function getControllers();
}
}
7 changes: 7 additions & 0 deletions src/psm/Module/Server/ServerModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@
**/

namespace psm\Module\Server;

use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ServerModule implements ModuleInterface {

public function load(ContainerBuilder $container) {

}

public function getControllers() {
return array(
'server' => __NAMESPACE__ . '\Controller\ServerController',
Expand Down
4 changes: 4 additions & 0 deletions src/psm/Module/User/Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ protected function executeSave() {
unset($clean['password_repeat']);

$this->db->save(PSM_DB_PREFIX.'users', $clean, array('user_id' => $this->getUser()->getUserId()));
$this->container->get('event')->dispatch(
\psm\Module\User\UserEvents::USER_EDIT,
new \psm\Module\User\Event\UserEvent($this->getUser()->getUserId())
);
if(isset($password)) {
$this->getUser()->changePassword($this->getUser()->getUserId(), $password);
}
Expand Down
14 changes: 14 additions & 0 deletions src/psm/Module/User/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,20 @@ protected function executeSave() {
unset($clean['password']); // password update is executed separately
$this->db->save(PSM_DB_PREFIX.'users', $clean, array('user_id' => $user_id));
$this->addMessage(psm_get_lang('users', 'updated'), 'success');

$event = \psm\Module\User\UserEvents::USER_EDIT;
} else {
// add user
$clean['password'] = ''; // password update is executed separately
$user_id = $this->db->save(PSM_DB_PREFIX.'users', $clean);
$this->addMessage(psm_get_lang('users', 'inserted'), 'success');

$event = \psm\Module\User\UserEvents::USER_ADD;
}
$this->container->get('event')->dispatch(
$event,
new \psm\Module\User\Event\UserEvent($user_id, $this->getUser()->getUserId())
);
if(isset($password)) {
$this->getUser()->changePassword($user_id, $password);
}
Expand Down Expand Up @@ -294,6 +302,12 @@ protected function executeDelete() {

$this->db->delete(PSM_DB_PREFIX . 'users', array('user_id' => $id,));
$this->db->delete(PSM_DB_PREFIX.'users_servers', array('user_id' => $id));

$this->container->get('event')->dispatch(
\psm\Module\User\UserEvents::USER_DELETE,
new \psm\Module\User\Event\UserEvent($id, $this->getUser()->getUserId())
);

$this->addMessage(psm_get_lang('users', 'deleted'), 'success');
} catch(\InvalidArgumentException $e) {
$this->addMessage(psm_get_lang('users', 'error_' . $e->getMessage()), 'error');
Expand Down
51 changes: 51 additions & 0 deletions src/psm/Module/User/Event/UserEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <[email protected]>
* @copyright Copyright (c) 2008-2015 Pepijn Over <[email protected]>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 3.2
**/

namespace psm\Module\User\Event;

use Symfony\Component\EventDispatcher\Event;

class UserEvent extends Event {

protected $user_id;

protected $user_id_by;

public function __construct($user_id, $user_id_by = null) {
$this->user_id = $user_id;
$this->user_id_by = $user_id_by;
}

public function getUserId() {
return $this->user_id;
}

public function getUserIdBy() {
return $this->user_id_by;
}
}
53 changes: 53 additions & 0 deletions src/psm/Module/User/EventListener/UserSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <[email protected]>
* @copyright Copyright (c) 2008-2015 Pepijn Over <[email protected]>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 3.2
**/

namespace psm\Module\User\EventListener;

use psm\Module\User\UserEvents;
use psm\Module\User\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class UserSubscriber implements EventSubscriberInterface {

public static function getSubscribedEvents() {
return array(
UserEvents::USER_ADD => array('onUserAdd', 0),
UserEvents::USER_EDIT => array('onUserEdit', 0),
UserEvents::USER_DELETE => array('onUserDelete', 0),
);
}

public function onUserAdd(UserEvent $event) {
}

public function onUserEdit(UserEvent $event) {
}

public function onUserDelete(UserEvent $event) {
}
}
48 changes: 48 additions & 0 deletions src/psm/Module/User/UserEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <[email protected]>
* @copyright Copyright (c) 2008-2015 Pepijn Over <[email protected]>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 3.2
**/

namespace psm\Module\User;

final class UserEvents {

/**
* @var string
*/
const USER_ADD = 'user.add';

/**
* @var string
*/
const USER_EDIT = 'user.edit';

/**
* @var string
*/
const USER_DELETE = 'user.delete';

}
Loading

0 comments on commit 8505a34

Please sign in to comment.