Skip to content

Commit b788b39

Browse files
jacob.embreeBalazs Dianiska
authored andcommitted
Issue #2423275 by jacob.embree: Regression fixes and coder review
1 parent f2d8bd4 commit b788b39

File tree

4 files changed

+112
-102
lines changed

4 files changed

+112
-102
lines changed

simplesamlphp_auth.inc

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ function _simplesaml_auth_user_register($authname) {
5858
global $user;
5959
global $_simplesamlphp_auth_as;
6060

61-
// First we check the admin settings for simpleSAMLphp and find out if we are allowed to register users.
61+
// First we check the admin settings for simpleSAMLphp and find out if we are
62+
// allowed to register users.
6263
if (variable_get('simplesamlphp_auth_registerusers', TRUE)) {
6364

6465
// We are allowed to register new users.
@@ -84,21 +85,21 @@ function _simplesaml_auth_user_register($authname) {
8485

8586
_simplesaml_auth_debug(t('Registered [%authname] with uid @uid', array(
8687
'%authname' => $authname,
87-
'@uid' => $user->uid
88+
'@uid' => $user->uid,
8889
)));
8990

9091
if (!empty($user->uid)) {
9192
// Populate roles based on configuration setting.
9293
$roles = _simplesamlphp_auth_rolepopulation(variable_get('simplesamlphp_auth_rolepopulation', ''));
9394
$userinfo = array('roles' => $roles);
94-
// @todo - Fjernet rolle-delen her da den gav en bra feilmelding når roller ikke finnes.
9595
$user = user_save($user, $userinfo);
9696

9797
return $user;
9898
}
9999
else {
100100
// We were unable to register this new user on the site.
101-
// We let the user know about this, log an error, and redirect to the home page.
101+
// We let the user know about this, log an error, and redirect to the home
102+
// page.
102103
drupal_set_message(t("We are sorry. While you have successfully authenticated, we were unable to create an account for you on this site. Please ask the site administrator to provision access for you."));
103104
watchdog('simplesamlphp_auth', 'Unable to register %authname using simplesamlphp_auth', array('%authname' => $authname), WATCHDOG_ERROR);
104105
$_simplesamlphp_auth_as->logout(base_path());
@@ -107,13 +108,13 @@ function _simplesaml_auth_user_register($authname) {
107108
else {
108109
// We are not allowed to register new users on the site through simpleSAML.
109110
// We let the user know about this and redirect to the user/login page.
110-
drupal_set_message(t("We are sorry. While you have successfully authenticated, you are not yet entitled to access this site. Please ask the site administrator to provision access for you."));
111+
drupal_set_message(t("We are sorry. Although you have successfully authenticated, you are not yet entitled to access this site. Please ask the site administrator to provide access for you."));
111112
$_simplesamlphp_auth_as->logout(base_path());
112113
}
113114
}
114115

115116
/**
116-
* Updates a SAML authenticated user's account with current username and email.
117+
* Updates a SAML-authenticated user's account with current username and email.
117118
*
118119
* @param object $account
119120
* The user account object to update.
@@ -129,7 +130,8 @@ function _simplesaml_auth_user_update($account) {
129130
// Get mail from default attribute.
130131
try {
131132
$mail_address = _simplesamlphp_auth_get_mail();
132-
} catch (Exception $e) {
133+
}
134+
catch (Exception $e) {
133135
drupal_set_message(t('Your e-mail address was not provided by your identity provider (IDP).'), "error");
134136
watchdog('simplesamlphp_auth', $e->getMessage(), NULL, WATCHDOG_CRITICAL);
135137
}
@@ -191,43 +193,48 @@ function simplesaml_auth_moderate_local_login() {
191193
if (!variable_get('simplesamlphp_auth_allowdefaultlogin', TRUE)) {
192194
// If the user has NOT been authenticated via simpleSAML...
193195
if (!$_simplesamlphp_auth_as->isAuthenticated()) {
194-
// :FYI: Until Drupal issue #754560 is corrected this message will never be seen by the user.
196+
// FYI: Until Drupal issue #754560 is corrected this message will never be
197+
// seen by the user.
195198
drupal_set_message(t("We are sorry, users are not permitted to log in using local accounts."));
196-
// Destroy the user's session (log them out).
199+
// Destroy the user's session (log out).
197200
_simplesamlphp_auth_destroy_drupal_session();
198201
}
199202
}
200203
// If we are allowing users to log in with local accounts.
201204
else {
202205
// If the user has NOT been authenticated via simpleSAML.
203206
if (!$_simplesamlphp_auth_as->isAuthenticated()) {
204-
// See if we limit this privilege to specified users
205-
$strAllwDefLogUsers = variable_get('simplesamlphp_auth_allowdefaultloginusers', '');
206-
$arrAllwDefLogUsers = array();
207+
// See if we limit this privilege to specified users.
208+
$str_users_allowed_local = variable_get('simplesamlphp_auth_allowdefaultloginusers', '');
207209
// See if we limit this privilege to specified roles.
208-
$arrAllwDefLogRoles = variable_get('simplesamlphp_auth_allowdefaultloginroles', FALSE);
210+
$array_roles_allowed_local = variable_get('simplesamlphp_auth_allowdefaultloginroles', array());
209211

210-
// If user IDs or roles are specified, we let them in, but everyone else gets logged out.
211-
if (drupal_strlen($strAllwDefLogUsers) || $arrAllwDefLogRoles) {
212+
// If user IDs or roles are specified, we let them in, but everyone else
213+
// gets logged out.
214+
if (drupal_strlen($str_users_allowed_local) || $array_roles_allowed_local) {
212215

213216
// Convert the string into an array.
214-
// @todo Perform a test to make sure that only numbers, spaces, or commas are in the string.
215-
$arrAllwDefLogUsers = explode(',', $strAllwDefLogUsers);
217+
// @todo Perform a test to make sure that only numbers, spaces, or
218+
// commas are in the string.
219+
$array_users_allowed_local = explode(',', $str_users_allowed_local);
216220

217221
// If we still have something to work with.
218-
if (0 < count($arrAllwDefLogUsers) || 0 < count($arrAllwDefLogRoles)) {
219-
/* Log the user out of Drupal if:
220-
1) the current user's uid is NOT in the list of allowed uids...
221-
2) or their role does not match and allowed mixed mode role. */
222-
$matchRoles = array_intersect(array_keys($user->roles), $arrAllwDefLogRoles);
223-
if (!in_array($user->uid, $arrAllwDefLogUsers) && count($matchRoles) == 0) {
224-
// User is logged into Drupal, but may not be logged into simpleSAML.
225-
// If this is the case we're supposed to log the user out of Drupal.
226-
227-
// :FYI: Until Drupal issue #754560 is corrected this message will never be seen by the user.
222+
if (0 < count($array_users_allowed_local) || 0 < count($array_roles_allowed_local)) {
223+
// Log the user out of Drupal if:
224+
// 1) the current user's uid is NOT in the list of allowed uids
225+
// 2) or their role does not match and allowed mixed mode role.
226+
$match_roles = array_intersect(array_keys($user->roles), $array_roles_allowed_local);
227+
if (!in_array($user->uid, $array_users_allowed_local) && count($match_roles) == 0) {
228+
// User is logged into Drupal, but may not be logged into
229+
// simpleSAML. If this is the case we're supposed to log the user
230+
// out of Drupal.
231+
232+
// FYI: Until Drupal issue #754560 is corrected this message will
233+
// never be seen by the user.
228234
drupal_set_message(t("We are sorry, you are not permitted to log in using a local account."));
229235

230-
// The least we can do is write something to the watchdog so someone will know what's happening.
236+
// The least we can do is write something to the watchdog so someone
237+
// will know what's happening.
231238
watchdog('simplesamlphp_auth', 'User %name not authorized to log in using local account.', array('%name' => $user->name));
232239

233240
_simplesamlphp_auth_destroy_drupal_session();
@@ -246,8 +253,8 @@ function simplesaml_auth_moderate_local_login() {
246253
* Return any attributes provided by the SAML IDP.
247254
*
248255
* @param $attribute
249-
* The attribute whose value to return. Can be skipped if all attribute
250-
* values are requested.
256+
* (optional) The attribute whose value to return. Can be skipped if all
257+
* attribute values are requested.
251258
*
252259
* @return
253260
* If an attribute was provided, the value of the attribute is returned.
@@ -263,10 +270,8 @@ function simplesamlphp_auth_get_attributes($attribute = NULL) {
263270
$result = NULL;
264271

265272
// If the specified attribute is set, grab it.
266-
if (isset($_simplesamlphp_auth_saml_attributes)) {
267-
if (isset($_simplesamlphp_auth_saml_attributes[$attribute])) {
268-
$result = $_simplesamlphp_auth_saml_attributes[$attribute];
269-
}
273+
if (isset($_simplesamlphp_auth_saml_attributes[$attribute])) {
274+
$result = $_simplesamlphp_auth_saml_attributes[$attribute];
270275
}
271276
}
272277

@@ -282,14 +287,14 @@ function simplesamlphp_auth_get_attributes($attribute = NULL) {
282287
}
283288
}
284289

285-
// Return whatever we've got.`
290+
// Return whatever we have.
286291
return $result;
287292
}
288293

289294
/**
290295
* Determine if the current user is authenticated through SAML.
291296
*
292-
* @return
297+
* @return bool
293298
* TRUE if the current user is authenticated through SAML. FALSE otherwise.
294299
*/
295300
function simplesamlphp_auth_is_authenticated() {
@@ -298,7 +303,8 @@ function simplesamlphp_auth_is_authenticated() {
298303
// Assume that the user isn't authenticated until proven otherwise.
299304
$authenticated = FALSE;
300305

301-
// If the associated global variable exists, and the auth flag is set, note it.
306+
// If the associated global variable exists, and the auth flag is set, note
307+
// it.
302308
if (isset($_simplesamlphp_auth_as) && $_simplesamlphp_auth_as->isAuthenticated()) {
303309
$authenticated = TRUE;
304310
}

simplesamlphp_auth.install

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* @file
5-
* the install file for the simplesamlphp_auth module
5+
* The install file for the simplesamlphp_auth module.
66
*/
77

88
/**
@@ -51,25 +51,26 @@ function simplesamlphp_auth_uninstall() {
5151
* Implements hook_requirements().
5252
*/
5353
function simplesamlphp_auth_requirements($phase) {
54+
$t = get_t();
5455
$requirements = array();
5556

5657
if ($phase == 'runtime') {
5758
if (!variable_get('simplesamlphp_auth_activate', 0)) {
5859
$requirements['simplesamlphp_auth'] = array(
5960
'severity' => REQUIREMENT_WARNING,
6061
'title' => 'SimpleSAMLphp auth',
61-
'value' => t('SimpleSAMLphp authentication is NOT activated'),
62-
'description' => t('It can be activated on the !admin_page.', array('!admin_page' => l(t('configuration page'), 'admin/config/people/simplesamlphp_auth'))),
63-
);
62+
'value' => $t('SimpleSAMLphp authentication is NOT activated'),
63+
'description' => $t('It can be activated on the !admin_page.', array('!admin_page' => l($t('configuration page'), 'admin/config/people/simplesamlphp_auth'))),
64+
);
6465
}
6566

6667
$basedir = variable_get('simplesamlphp_auth_installdir', '/var/simplesamlphp');
6768
if (!file_exists($basedir . '/lib/_autoload.php')) {
6869
$requirements['simplesamlphp_auth'] = array(
6970
'severity' => REQUIREMENT_ERROR,
7071
'title' => 'SimpleSAMLphp auth',
71-
'value' => t('SimpleSAMLphp authentication is missing the required SimpleSAMLphp library'),
72-
'description' => t('Please download and install the !simplesamlphp library.', array('!simplesamlphp' => l(t('SimpeSAMLphp'), 'https://simplesamlphp.org/download'))),
72+
'value' => $t('SimpleSAMLphp authentication is missing the required SimpleSAMLphp library'),
73+
'description' => $t('Please download and install the !simplesamlphp library.', array('!simplesamlphp' => l($t('SimpeSAMLphp'), 'https://simplesamlphp.org/download'))),
7374
);
7475
}
7576
}

0 commit comments

Comments
 (0)