forked from LukeTowers/oc-azureadsso-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
138 lines (122 loc) · 4.53 KB
/
Plugin.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php namespace LukeTowers\AzureADSSO;
use App;
use View;
use Event;
use Config;
use Request;
use Backend\Models\UserRole;
use System\Classes\PluginBase;
use System\Classes\CombineAssets;
use Illuminate\Foundation\AliasLoader;
use Backend\Controllers\Auth as AuthController;
/**
* AzureADSSO Plugin Information File
*/
class Plugin extends PluginBase
{
public $elevated = true;
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'AzureAD SSO',
'description' => 'Adds support for logging into the backend with Azure AD SSO OAuth',
'author' => 'LukeTowers',
'icon' => 'icon-lock'
];
}
/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
$this->app->bind('Illuminate\Contracts\Auth\Factory', function () {
return \Backend\Classes\AuthManager::instance();
});
AuthController::extend(function($controller) {
$controller->bindEvent('page.beforeDisplay', function ($action, $params) use ($controller) {
if ($action === 'signin') {
$controller->addCss(CombineAssets::combine(['azureadsso.css'], plugins_path('luketowers/azureadsso/assets/css/')));
}
});
});
Event::listen('backend.auth.extendSigninView', function ($controller) {
return View::make("luketowers.azureadsso::login");
});
$this->bootPackages();
$this->extendAzureAD();
}
/**
* Boots (configures and registers) any packages found within this plugin's packages.load configuration value
*
* @see https://luketowers.ca/blog/how-to-use-laravel-packages-in-october-plugins
* @author Luke Towers <[email protected]>
*/
public function bootPackages()
{
// Get the namespace of the current plugin to use in accessing the Config of the plugin
$pluginNamespace = str_replace('\\', '.', strtolower(__NAMESPACE__));
// Instantiate the AliasLoader for any aliases that will be loaded
$aliasLoader = AliasLoader::getInstance();
$hostname = parse_url(Request::url(), PHP_URL_HOST);
$subDomain = explode('.', $hostname)[0];
// Get the packages to boot
$packages = Config::get($pluginNamespace . '::' . $subDomain, '');
if (empty($packages)) {
$packages = Config::get($pluginNamespace . '::packages');
}
// Boot each package
foreach ($packages as $name => $options) {
// Setup the configuration for the package, pulling from this plugin's config
if (!empty($options['config']) && !empty($options['config_namespace'])) {
Config::set($options['config_namespace'], $options['config']);
}
// Register any Service Providers for the package
if (!empty($options['providers'])) {
foreach ($options['providers'] as $provider) {
App::register($provider);
}
}
// Register any Aliases for the package
if (!empty($options['aliases'])) {
foreach ($options['aliases'] as $alias => $path) {
$aliasLoader->alias($alias, $path);
}
}
}
}
/**
* Extend the base library used to make it compatible with OctoberCMS
*
* @return void
*/
protected function extendAzureAD()
{
// Process the user object before saving it
\Metrogistics\AzureSocialite\UserFactory::userCallback(function($newUser) {
// Generate a random password for the user
$pass = str_random(60);
$newUser->password = $pass;
$newUser->password_confirmation = $pass;
// Ensure that the user has an email address
if (empty($newUser->email) && !empty($newUser->alt_email)) {
$newUser->email = $newUser->alt_email;
}
if (empty($newUser->login)) {
$newUser->login = $newUser->email;
}
// Assign the default role if provided
if ($code = Config::get('azure-oath.default_role_code')) {
$newUser->role_id = UserRole::select('id', 'code')->where('code', $code)->first()->id;
}
// Clean up
unset($newUser->attributes['alt_email']);
});
}
}