Describe operators' events in one place

This commit is contained in:
Dmitriy Simushev 2014-10-17 10:54:04 +00:00
parent 55b7b28eb1
commit 2dca8f236e
3 changed files with 79 additions and 18 deletions

View File

@ -20,6 +20,7 @@
namespace Mibew\Authentication; namespace Mibew\Authentication;
use Mibew\EventDispatcher\EventDispatcher; use Mibew\EventDispatcher\EventDispatcher;
use Mibew\EventDispatcher\Events;
use Mibew\Http\CookieFactory; use Mibew\Http\CookieFactory;
use Mibew\Http\CookieFactoryAwareInterface; use Mibew\Http\CookieFactoryAwareInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -82,12 +83,8 @@ class AuthenticationManager implements AuthenticationManagerInterface, CookieFac
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* Triggers 'operatorAuthenticate' event if operator is not authenticated by * Triggers {@link \Mibew\EventDispatcher\Events::OPERATOR_AUTHENTICATE}
* the system and pass to it an associative array with following items: * event.
* - 'operator': if a plugin has extracted operator from the request it
* should set operator's data to this field.
* - 'request': {@link Request}, incoming request. Can be used by a plugin
* to extract an operator.
*/ */
public function setOperatorFromRequest(Request $request) public function setOperatorFromRequest(Request $request)
{ {
@ -123,7 +120,7 @@ class AuthenticationManager implements AuthenticationManagerInterface, CookieFac
'request' => $request, 'request' => $request,
); );
$dispatcher = EventDispatcher::getInstance(); $dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('operatorAuthenticate', $args); $dispatcher->triggerEvent(Events::OPERATOR_AUTHENTICATE, $args);
if (!empty($args['operator'])) { if (!empty($args['operator'])) {
// Cache operator in the session // Cache operator in the session
@ -210,10 +207,7 @@ class AuthenticationManager implements AuthenticationManagerInterface, CookieFac
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* Triggers 'operatorLogin' event after operator logged in and pass to it an * Triggers {@link \Mibew\EventDispatcher\Events::OPERATOR_LOGIN} event.
* associative array with following items:
* - 'operator': array of the logged in operator info;
* - 'remember': boolean, indicates if system should remember operator.
*/ */
public function loginOperator($operator, $remember) public function loginOperator($operator, $remember)
{ {
@ -228,13 +222,13 @@ class AuthenticationManager implements AuthenticationManagerInterface, CookieFac
'remember' => $remember, 'remember' => $remember,
); );
$dispatcher = EventDispatcher::getInstance(); $dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('operatorLogin', $args); $dispatcher->triggerEvent(Events::OPERATOR_LOGIN, $args);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* Triggers 'operatorLogout' event after operator logged out. * Triggers {@link \Mibew\EventDispatcher\Events::OPERATOR_LOGOUT} event.
*/ */
public function logoutOperator() public function logoutOperator()
{ {
@ -246,6 +240,6 @@ class AuthenticationManager implements AuthenticationManagerInterface, CookieFac
// Trigger logout event // Trigger logout event
$dispatcher = EventDispatcher::getInstance(); $dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('operatorLogout'); $dispatcher->triggerEvent(Events::OPERATOR_LOGOUT);
} }
} }

View File

@ -0,0 +1,68 @@
<?php
/*
* This file is a part of Mibew Messenger.
*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\EventDispatcher;
/**
* This class contains a list of events that are present in the system.
*/
final class Events
{
/**
* An operator cannot be authenticated.
*
* This event is triggered if an operator cannot be authenticated by the
* system. It provides an ability for plugins to implement custom
* authentication logic. An associative array with the following items is
* passed to the event handlers:
* - "operator": array, if a plugin has extracted operator from the request
* it should set operator's data to this field.
* - "request": {@link \Symfony\Component\HttpFoundation\Request},
* incoming request. Can be used by a plugin to extract an operator.
*/
const OPERATOR_AUTHENTICATE = 'operatorAuthenticate';
/**
* An operator logged in.
*
* This event is triggered after an operator logged in using system login
* form. An associative array with the following items is passed to the
* event handlers:
* - "operator": array of the logged in operator info;
* - "remember": boolean, indicates if system should remember operator.
*/
const OPERATOR_LOGIN = 'operatorLogin';
/**
* An operator logged out.
*
* This event is triggered after an operator is logged out.
*/
const OPERATOR_LOGOUT = 'operatorLogout';
/**
* An operator is deleted.
*
* This event is triggered after an operator has been deleted. An
* associative array with the following items is passed to the event
* handlers:
* - "id": int, deleted operator ID.
*/
const OPERATOR_DELETE = 'operatorDelete';
}

View File

@ -20,6 +20,7 @@
// Import namespaces and classes of the core // Import namespaces and classes of the core
use Mibew\Database; use Mibew\Database;
use Mibew\EventDispatcher\EventDispatcher; use Mibew\EventDispatcher\EventDispatcher;
use Mibew\EventDispatcher\Events;
use Mibew\Settings; use Mibew\Settings;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@ -410,9 +411,7 @@ function create_operator(
* *
* This function remove operator and associations with groups for this operator * This function remove operator and associations with groups for this operator
* from datatabse. * from datatabse.
* It triggers 'operatorDelete' event and pass to event listeners associative * It triggers {@link \Mibew\EventDispatcher\Events::OPERATOR_DELETE} event.
* array with following keys:
* - 'id': int, deleted operator ID.
* *
* @param int $operator_id Operator ID * @param int $operator_id Operator ID
*/ */
@ -431,7 +430,7 @@ function delete_operator($operator_id)
// Trigger 'operatorDelete' event // Trigger 'operatorDelete' event
$dispatcher = EventDispatcher::getInstance(); $dispatcher = EventDispatcher::getInstance();
$args = array('id' => $operator_id); $args = array('id' => $operator_id);
$dispatcher->triggerEvent('operatorDelete', $args); $dispatcher->triggerEvent(Events::OPERATOR_DELETE, $args);
} }
/** /**