-
Notifications
You must be signed in to change notification settings - Fork 0
/
MantisDiscourseSSO.php
83 lines (71 loc) · 2.09 KB
/
MantisDiscourseSSO.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
<?php
require_once(config_get('class_path').'MantisPlugin.class.php' );
class MantisDiscourseSSOPlugin extends MantisPlugin
{
function register()
{
$this->name = 'Discourse SSO Plugin';
$this->description = 'Discourse SSO authentication for MantisBT.';
$this->version = '1.0';
$this->requires = array(
'MantisCore' => '1.2.0',
);
$this->author = 'Arseniy Shestakov';
$this->contact = '[email protected]';
$this->url = 'https://github.com/ArseniyShestakov/MantisDiscourseSSO';
}
function init()
{
require_once(config_get('absolute_path').'discourse-sso.php');
}
function install()
{
return true;
}
function hooks()
{
return array(
'EVENT_CORE_READY' => 'login',
'EVENT_LAYOUT_PAGE_HEADER' => 'loginPage'
);
}
function login()
{
if(auth_is_user_authenticated() && !current_user_is_anonymous())
return;
if(DISCOURSE_SSO !== config_get('login_method'))
return;
$DISCOURSE_SSO = new DiscourseSSOClient(true);
$SSO_STATUS = $DISCOURSE_SSO->getAuthentication();
if(true !== $SSO_STATUS['logged'] || empty($SSO_STATUS['data']['username']))
return;
$DISCOURSE_SSO->removeNonce($SSO_STATUS['nonce']);
$userId = user_get_id_by_name($SSO_STATUS['data']['username']);
if(false === $userId)
{
user_create($SSO_STATUS['data']['username'], '');
$userId = user_get_id_by_name($SSO_STATUS['data']['username']);
if(false === $userId)
{
trigger_error('Discourse SSO: cant create user!');
}
}
user_set_email($userId, $SSO_STATUS['data']['email']);
if(!empty($SSO_STATUS['data']['name']))
user_set_realname($userId, $SSO_STATUS['data']['name']);
user_increment_login_count($userId);
user_reset_failed_login_count_to_zero($userId);
user_reset_lost_password_in_progress_count_to_zero($userId);
auth_set_cookies($userId, true);
auth_set_tokens($userId);
print_header_redirect('view_all_bug_page.php');
}
function loginPage()
{
if('login_page.php' !== basename($_SERVER['PHP_SELF']))
return;
if(DISCOURSE_SSO !== config_get('login_method'))
return;
print_header_redirect('discourse-sso.php');
}
}