-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-sm-auth.php
183 lines (150 loc) · 4.94 KB
/
class-sm-auth.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Plugin class.
*
* @package SM_Auth
*/
class SM_Auth {
const VERSION = '1.0.0';
public $_isAuthenticated = false;
public $plugin_slug = 'sm-auth';
private static $instance = null;
private $plugin_screen_hook_suffix = null;
// private $universalid = 'ROLARIU'; //fake it 'till we make it
private $universalid = '';
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* If user not logged in, use SM login
*/
public function authenticate() {
if($this->is_user_logged_in() || !$this->universalid) {
return;
}
$userid = username_exists($this->universalid);
if(!$userid) {
$userid = $this->create_user($this->universalid);
}
$this->login($userid);
}
private function is_user_logged_in() {
return get_current_user_id() != 0;
}
private function login($userid) {
wp_set_auth_cookie($userid);
}
/**
* Create a new WordPress account for the specified username.
*
* @param string $username
* @return integer user_id
*/
private function create_user($username) {
$password = wp_generate_password( $length=12, $include_standard_special_chars=false );
return wp_create_user($username, $password);
}
/**
* Generate a random password.
*
* @param int $length Length of the password
* @return password as string
*/
private function _get_password($length = 10) {
return substr(md5(uniqid(microtime())), 0, $length);
}
private function __construct() {
$this->universalid = $_SERVER["HTTP_USER_ID"];
// Add the options page and menu item.
add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
// Add an action link pointing to the options page.
$plugin_basename = plugin_basename( plugin_dir_path( __FILE__ ) . 'sm-auth.php' );
add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) );
// Load admin style sheet and JavaScript.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
add_filter('init', array($this, 'authenticate'), 30);
}
/**
* Fired when the plugin is activated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
*/
public static function activate( $network_wide ) {
// TODO: Define activation functionality here
}
/**
* Fired when the plugin is deactivated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
*/
public static function deactivate( $network_wide ) {
// TODO: Define deactivation functionality here
}
/**
* Register and enqueue admin-specific style sheet.
*
* @return null Return early if no settings page is registered.
*/
public function enqueue_admin_styles() {
if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
return;
}
$screen = get_current_screen();
if ( $screen->id == $this->plugin_screen_hook_suffix ) {
wp_enqueue_style( $this->plugin_slug .'-admin-styles', plugins_url( 'css/admin.css', __FILE__ ), array(), $this->version );
}
}
/**
* Register and enqueue admin-specific JavaScript.
*
* @return null Return early if no settings page is registered.
*/
public function enqueue_admin_scripts() {
if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
return;
}
$screen = get_current_screen();
if ( $screen->id == $this->plugin_screen_hook_suffix ) {
wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery' ), $this->version );
}
}
/**
* Register the administration menu for this plugin into the WordPress Dashboard menu.
*/
public function add_plugin_admin_menu() {
$this->plugin_screen_hook_suffix = add_options_page(
__( 'SiteMinder Authentication Page', $this->plugin_slug ),
__( 'SiteMinder Authentication', $this->plugin_slug ),
'read',
$this->plugin_slug,
array( $this, 'display_plugin_admin_page' )
);
}
/**
* Render the settings page for this plugin.
*/
public function display_plugin_admin_page() {
include_once( 'views/admin.php' );
}
/**
* Add settings action link to the plugins page.
*/
public function add_action_links( $links ) {
return array_merge(
array(
'settings' => '<a href="' . admin_url( 'plugins.php?page=sm-auth' ) . '">' . __( 'Settings', $this->plugin_slug ) . '</a>'
),
$links
);
}
}