This repository has been archived by the owner on Mar 5, 2019. It is now read-only.
/
GoogleAuthenticator.php
executable file
·88 lines (78 loc) · 2.57 KB
/
GoogleAuthenticator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\GoogleAuthenticator;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\FrontController;
use Piwik\Notification;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
/**
*
*/
class GoogleAuthenticator extends \Piwik\Plugins\Login\Login
{
/**
* Set login name and authentication token for API request.
* Listens to API.Request.authenticate hook.
*/
public function ApiRequestAuthenticate($tokenAuth)
{
/** @var \Piwik\Auth $auth */
$auth = StaticContainer::get('Piwik\Auth');
$auth->setLogin($login = null);
$auth->setTokenAuth($tokenAuth);
$auth->setAuthCode(Common::getRequestVar('auth_code', '', 'string'));
}
/**
* Redirects to Login form with error message.
* Listens to User.isNotAuthorized hook.
*/
public function noAccess(\Exception $exception)
{
$frontController = FrontController::getInstance();
if (Common::isXmlHttpRequest()) {
echo $frontController->dispatch('GoogleAuthenticator', 'ajaxNoAccess', array($exception->getMessage()));
return;
}
echo $frontController->dispatch('GoogleAuthenticator', 'login', array($exception->getMessage()));
}
public function getJsFiles(&$javascriptFiles)
{
parent::getJsFiles($javascriptFiles);
$javascriptFiles[] = "plugins/GoogleAuthenticator/angularjs/settings.controller.js";
}
public function postLoad()
{
$this->activate();
}
/**
* Deactivate default Login module, as both cannot be activated together
*
* TODO: shouldn't disable Login plugin but have to wait until Dependency Injection is added to core
*/
public function activate()
{
if (Manager::getInstance()->isPluginActivated("Login") == true) {
Manager::getInstance()->deactivatePlugin("Login");
$notification = new Notification(Piwik::translate('GoogleAuthenticator_LoginPluginDisabled'));
$notification->context = Notification::CONTEXT_INFO;
Notification\Manager::notify('GoogleAuthenticator_LoginPluginDisabled', $notification);
}
}
/**
* Activate default Login module, as one of them is needed to access Piwik
*/
public function deactivate()
{
if (Manager::getInstance()->isPluginActivated("Login") == false) {
Manager::getInstance()->activatePlugin("Login");
}
}
}