$key, "status" => 1)); return $user; } function sess_write($key, $value) { global $HTTP_SERVER_VARS; db_query("UPDATE user SET hostname = '". check_input($HTTP_SERVER_VARS[REMOTE_ADDR]) ."', timestamp = '". time() ."' WHERE session = '$key'"); } function sess_destroy($key) { global $HTTP_SERVER_VARS; db_query("UPDATE user SET hostname = '". check_input($HTTP_SERVER_VARS[REMOTE_ADDR]) ."', timestamp = '". time() ."', session = '' WHERE session = '$key'"); } function sess_gc($lifetime) { return 1; } /*** Common functions ******************************************************/ function user_load($array = array()) { /* ** Dynamically compose a SQL query: */ foreach ($array as $key => $value) { if ($key == "pass") { $query .= "u.$key = PASSWORD('". addslashes($value) ."') AND "; } else { $query .= "u.$key = '". addslashes($value) ."' AND "; } } $result = db_query("SELECT u.*, r.perm FROM user u LEFT JOIN role r ON u.role = r.name WHERE $query u.status < 3"); return db_fetch_object($result); } function user_save($account, $array = array()) { /* ** Dynamically compose a SQL query: */ foreach ($array as $key => $value) { if ($key == "pass") { $query .= "$key = PASSWORD('". addslashes($value) ."'), "; } else { $query .= "$key = '". addslashes($value) ."', "; } } /* ** Update existing or insert new user account: */ if ($account->uid) { db_query("UPDATE user SET $query timestamp = '". time() ."' WHERE uid = '$account->uid'"); return user_load(array("uid" => $account->uid)); } else { db_query("INSERT INTO user SET $query timestamp = '". time() ."'"); return user_load(array("name" => $array["name"])); } } function user_set($account, $key, $value) { $account->data[$key] = $value; return $account; } function user_get($account, $key) { return $account->data[$key]; } function user_validate_name($name) { /* ** Verify the syntax of the given name: */ if (!$name) return t("You must enter a name."); if (eregi("^ ", $name)) return t("The name can not begin with a space."); if (eregi(" \$", $name)) return t("The name can not end with a space."); if (eregi(" ", $name)) return t("The name can not contain multiple spaces in a row."); if (eregi("[^a-zA-Z0-9 ]", $name)) return t("The name contains an illegal character."); if (strlen($name) > 32) return t("The name '$name' is too long: it must be less than 32 characters."); } function user_validate_mail($mail) { /* ** Verify the syntax of the given e-mail address: */ if (!$mail) return t("Your must enter an e-mail address."); if (!eregi("^[_+\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $mail)) return t("The e-mail address '$mail' is not valid."); } function user_password($min_length = 6) { /* ** Generate a human-readable password: */ mt_srand((double)microtime() * 1000000); $words = explode(",", variable_get("user_password", "foo,bar,guy,neo,tux,moo,sun,asm,dot,god,axe,geek,nerd,fish,hack,star,mice,warp,moon,hero,cola,girl,fish,java,perl,boss,dark,sith,jedi,drop,mojo")); while (strlen($password) < $min_length) $password .= trim($words[mt_rand(0, count($words))]); return $password; } function user_access($perm) { global $user; if ($user->uid == 1) { return 1; } else if ($user->perm) { return strstr($user->perm, $perm); } else { return db_fetch_object(db_query("SELECT * FROM role WHERE name = 'anonymous user' AND perm LIKE '%$perm%'")); } } function user_mail($mail, $subject, $message, $header) { // print "
subject: $subject"; mail($mail, $subject, $message, $header); } function user_deny($type, $mask) { $allow = db_fetch_object(db_query("SELECT * FROM access WHERE status = '1' AND type = '$type' AND LOWER('$mask') LIKE LOWER(mask)")); $deny = db_fetch_object(db_query("SELECT * FROM access WHERE status = '0' AND type = '$type' AND LOWER('$mask') LIKE LOWER(mask)")); if ($deny && !$allow) { return 1; } else { return 0; } } /*** Module hooks **********************************************************/ function user_help() { ?>
header: $header
$message
The user system is responsible for maintaining the user database. It automatically handles tasks like registration, authentication, access control, password retrieval, user settings and much more.
No participant can use his own name or handle to post comments until they sign up and submit their e-mail address. Those who do not may participate as anonymous users, but they will suffer numerous disadvantages, for example their posts beginning at a lower score.
In contrast, those with a user account can use their own name or handle and are granted various privileges: the most important are probably the ability to post content, to rate comments and to fine-tune the site to their personal liking.
Registered users need to authenticate by supplying a username and password, or alternatively, by supplying a valid Drupal ID or Jabber ID. The username and password are kept in the database, where the password is hashed so that no one can read nor use it. When a username and password needs to be checked the system goes down the list of registered users till it finds a matching username, and then hashes the password that was supplied and compares it to the listed value. If they match then that means the username and password supplied were correct.
Once a user authenticated a session is started and until that session is over they won't have to re-authenticate. To keep track of the individual sessions, drupal relies on PHP's session support. A visitor accessing your web site is assigned an unique ID, the so-called session ID, which is stored in a cookie. For security's sake, the cookie does not contain personal information but acts as a key to retrieve the information stored on your server's side. When a visitor accesses your site, drupal will check whether a specific session ID has been sent with the request. If this is the case, the prior saved environment is recreated.
Drupal allows you to control who is allowed to get authenticated and who is not. To accomplish this, you can ban certain hostnames, IPs, IP-ranges, e-mail address and usernames. Any user that matches any of the given ban criteria will not be able to authenticate or to register as a new user.
Authenticated users can themselves select entirely different appearances for the site, utilizing their own preferences for how the pages are structured, how navigation lists and other page components are presented and much more.
An important feature of drupal is that any user can be granted administrator rights. The ability to share maintenance responsibility with volunteers from across the globe can be considered valuable for most community-based projects.
The access rules allows you to describe which e-mail addresses or usernames will or will not be granted access using a flexible wild-card system. Examples are given below. If no access rules are provided, access control is turned off and everybody will be able to access your website. For each category, zero or more access rules can be specified. The 'allow' rules are processed prior to the 'deny' rules and are thus considered to be stronger.
To do describe access rules you can use the following wild-card characters:
Examples:
%@hotmail.com
, %@altavista.%
, %@usa.net
, etc. Used to prevent users from using free email accounts, which might be used to cause trouble.root
, webmaster
, admin%
, etc. Used to prevent administrator impersonators.Users have roles that define what kinds of actions they can take. Roles define classes of users such as anonymous user, authenticated user, moderator, administrator and so on. Every user can have one role.
Roles make it easier for you to manage security. Instead of defining what every single user can do, you can simply set a couple different permissions for different user roles.
Drupal comes with three built-in roles:
For basic Drupal sites you can get by with anonymous user and authenticated user but for more complex sites where you want other users to be able to perform maintainance or administrative duties, you may want to create your own roles to classify your users into different groups.
Each Drupal's permission describes a fine-grained logical operation such as access administration pages or add and modify user accounts. You could say a permission represents access granted to a user to perform a set of operations.
Roles tie users to permissions. The combination of roles and permissions represent a way to tie user authorization to the performance of actions, which is how Drupal can determine what users can do.
$account->name, "link" => (strstr($PHP_SELF, "admin.php") ? "admin.php?mod=user&op=edit&id=$account->uid" : "module.php?mod=user&op=view&id=$account->uid"), "user" => $account->name); } return $find; } function user_link($type) { if ($type == "page") { $links[] = "". t("user account") .""; } if ($type == "menu") { $links[] = "". t("account settings") .""; } if ($type == "admin") { $links[] = "user system"; } return $links ? $links : array(); } function drupal_login($arguments) { $argument = $arguments->getparam(0); $username = $argument->scalarval(); $argument = $arguments->getparam(1); $password = $argument->scalarval(); if ($user = user_load(array(name => "$username", "pass" => $password, "status" => 1))) { return new xmlrpcresp(new xmlrpcval($user->uid, "int")); } else { return new xmlrpcresp(new xmlrpcval(0, "int")); } } function user_xmlrpc() { return array("drupal.login" => array("function" => "drupal_login")); } /*** Authentication methods ************************************************/ function startElement($parser, $name, $attributes) { global $jabber; if ($attributes["ID"]) { $jabber["jid"] = $attributes["ID"]; } if (stristr($name, "error") || ($attributes["ERROR"])){ $jabber["error"] = true; } } function endElement($parser, $name) { } function characterData($parser, $data) { global $jabber; $jabber["data"] = $data; } function jabber_send($session, $message) { // print "SEND: ". htmlentities($message) ."" . htmlentities($message->serialize()) . ""; $client = new xmlrpc_client("/xmlrpc.php", $server, 80); $result = $client->send($message); if ($result && !$result->faultCode()) { $value = $result->value(); $login = $value->scalarval(); } return $login; } /*** User features *********************************************************/ function user_login($edit = array()) { global $user, $HTTP_REFERER; if (user_deny("user", $edit["name"])) { $error = sprintf(t("The name '%s' has been denied access."), $edit["name"]); } else if ($edit["name"] && $edit["pass"]) { /* ** Strip name and server from ID: */ if ($pos = strpos($edit["name"], "@")) { $server = check_input(substr($edit["name"], $pos + 1, strlen($edit["name"]))); $name = check_input(substr($edit["name"], 0, $pos)); $pass = check_input($edit["pass"]); } else { $name = check_input($edit["name"]); $pass = check_input($edit["pass"]); } /* ** Try to log on the user locally: */ if (!$user) { $user = user_load(array("name" => $name, "pass" => $pass, "status" => 1)); } /* ** Try to log on the user through Drupal: */ if (!$user && $server && variable_get("user_drupal", 1) == 1 && $id = drupal_auth($name, $pass, $server)) { $user = user_load(array("drupal" => "$id@$server", "status" => 1)); if (!$user && variable_get("user_register", 1) == 1 && !user_load(array("name" => $name))) { watchdog("user", "new user: '$name' <$name@$server> (Drupal ID)"); $user = user_save("", array("name" => $name, "pass" => user_password(), "init" => "$id@$server", "drupal" => "$id@$server", "role" => "authenticated user", "status" => 1)); } } /* ** Try to log on the user through Jabber: */ if (!$user && $server && variable_get("user_jabber", 1) == 1 && jabber_auth($name, $pass, $server)) { $user = user_load(array("jabber" => "$name@$server", "status" => 1)); if (!$user && variable_get("user_register", 1) == 1 && !user_load(array("name" => "$name"))) { watchdog("user", "new user: '$name' <$name@$server> (Jabber ID)"); $user = user_save("", array("name" => $name, "pass" => user_password(), "init" => "$name@$server", "jabber" => "$name@$server", "role" => "authenticated user", "status" => 1)); } } if ($user->uid) { watchdog("user", "session opened for '$user->name'"); /* ** Write session ID to database: */ user_save($user, array("session" => session_id())); /* ** Redirect the user to the page he logged on from or to his personal ** information page if we can detect the referer page: */ $url = $HTTP_REFERER ? $HTTP_REFERER : "module.php?mod=user&op=view"; drupal_goto($url); } else { watchdog("user", "failed to login for '". $name ."': invalid password"); $error = t("Authentication failed."); } } /* ** Display error message (if any): */ if ($error) { $output .= "
". check_output($error) ."
"; } /* ** Display login form: */ $output .= form_textfield(t("Username"), "name", $edit["name"], 20, 64, t("Enter your local username, a Drupal ID or a Jabber ID.")); $output .= form_password(t("Password"), "pass", $pass, 20, 64, t("Enter the password that accompanies your username.")); $output .= form_submit(t("Log in")); return form($output); } function user_logout() { global $user; if ($user->uid) { watchdog("user", "session closed for user '$user->name'"); /* ** Destroy the current session: */ session_destroy(); unset($user); /* ** Redirect the user to his personal information page: */ drupal_goto("index.php"); } } function user_pass($edit = array()) { if ($edit["name"] && $edit["mail"]) { if ($account = db_fetch_object(db_query("SELECT uid FROM user WHERE name = '". check_input($edit["name"]) ."' AND mail = '". check_input($edit["mail"]) ."'"))) { $from = variable_get("site_mail", "root@localhost"); $pass = user_password(); /* ** Save new password: */ user_save($account, array("pass" => $pass)); /* ** Mail new passowrd: */ user_mail($edit["mail"], t("user account details"), sprintf(t("%s,\n\nyou requested us to e-mail you a new password for your account at %s. You can now login using the following username and password:\n\n username: %s\n password: %s\n\n\n-- %s team"), $edit["name"], variable_get("site_name", "drupal"), $edit["name"], $pass, variable_get("site_name", "drupal")), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from"); watchdog("user", "mail password: '". $edit["name"] ."' <". $edit["mail"] .">"); return t("Your password and further instructions have been sent to your e-mail address."); } else { watchdog("user", "mail password: '". $edit["name"] ."' and <". $edit["mail"] ."> do not match"); return t("Could not sent password: no match for the specified username and e-mail address."); } } else { /* ** Display form: */ $output .= form_textfield(t("Username"), "name", $edit["name"], 30, 64); $output .= form_textfield(t("E-mail address"), "mail", $edit["mail"], 30, 64); $output .= form_submit(t("E-mail new password")); return form($output); } } function user_register($edit = array()) { if ($edit["name"] && $edit["mail"]) { if ($error = user_validate_name($edit["name"])) { // do nothing } else if ($error = user_validate_mail($edit["mail"])) { // do nothing } else if (user_deny("user", $edit["name"])) { $error = sprintf(t("The name '%s' has been denied access."), $edit["name"]); } else if (user_deny("mail", $edit["mail"])) { $error = sprintf(t("The e-mail address '%s' has been denied access."), $edit["mail"]); } else if (db_num_rows(db_query("SELECT name FROM user WHERE LOWER(name) = LOWER('". $edit["name"] ."')")) > 0) { $error = sprintf(t("The name '%s' is already taken."), $edit["name"]); } else if (db_num_rows(db_query("SELECT mail FROM user WHERE LOWER(mail) = LOWER('". $edit["mail"] ."')")) > 0) { $error = sprintf(t("The e-mail address '%s' is already taken."), $edit["mail"]); } else if (variable_get("user_register", 1) == 0) { $error = t("Public registrations have been disabled by the site administrator."); } else { $success = 1; } } if ($success) { watchdog("user", "new user: '". $edit["name"] ."' <". $edit["mail"] .">"); $from = variable_get("site_mail", "root@localhost"); $pass = user_password(); if (variable_get("user_register", 1) == 1) { /* ** Create new user account, no administrator approval required: */ user_save("", array("name" => $edit["name"], "pass" => $pass, "init" => $edit["mail"], "mail" => $edit["mail"], "role" => "authenticated user", "status" => 1)); user_mail($edit["mail"], t("user account details"), sprintf(t("%s,\n\nsomoneone signed up for a user account on %s and supplied this e-mail address as their contact. If it wasn't you, just ignore this mail but if it was you, you can now login using the following username and password:\n\n username: %s\n password: %s\n\n\n-- %s team"), $edit["name"], variable_get("site_name", "drupal"), $edit["name"], $pass, variable_get("site_name", "drupal")), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from"); } else { /* ** Create new user account, administrator approval required: */ user_save("", array("name" => $edit["name"], "pass" => $pass, "init" => $edit["mail"], "mail" => $edit["mail"], "role" => "authenticated user", "status" => 0)); user_mail($edit["mail"], t("user account details"), sprintf(t("%s,\n\nsomoneone signed up for a user account on %s and supplied this e-mail address as their contact. If it wasn't you, just ignore this mail but if it was you, you can login as soon a site administrator approved your request using the following username and password:\n\n username: %s\n password: %s\n\n\n-- %s team"), $edit["name"], variable_get("site_name", "drupal"), $edit["name"], $pass, variable_get("site_name", "drupal")), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from"); } return t("Your password and further instructions have been sent to your e-mail address."); } else { if ($error) { $output .= "". check_output($error) ."
"; } $output .= form_textfield(t("Username"), "name", $edit["name"], 30, 64, t("Your full name or your prefered username: only letters, numbers and spaces are allowed.")); $output .= form_textfield(t("E-mail address"), "mail", $edit["mail"], 30, 64, t("Your e-mail address: a password and instructions will be sent to this e-mail address so make sure it is accurate.")); $output .= form_submit(t("Create new account")); return form($output); } } function user_edit($edit = array()) { global $HTTP_HOST, $themes, $user, $languages; if ($user->uid) { if ($edit["name"]) { if ($error = user_validate_name($edit["name"])) { // do nothing } else if ($error = user_validate_mail($edit["mail"])) { // do nothing } else if (db_num_rows(db_query("SELECT uid FROM user WHERE uid != '$user->uid' AND LOWER(name) = LOWER('". $edit["name"] ."')")) > 0) { $error = sprintf(t("The name '%s' is already taken."), $edit["name"]); } else if ($edit["mail"] && db_num_rows(db_query("SELECT uid FROM user WHERE uid != '$user->uid' AND LOWER(mail) = LOWER('". $edit["mail"] ."')")) > 0) { $error = sprintf(t("The e-mail address '%s' is already taken."), $edit["mail"]); } else if ($edit["jabber"] && db_num_rows(db_query("SELECT uid FROM user WHERE uid != '$user->uid' AND LOWER(jabber) = LOWER('". $edit["jabber"] ."')")) > 0) { $error = sprintf(t("The Jabber ID '%s' is already taken."), $edit["jabber"]); } else if ($user->uid) { /* ** Save user information: */ $user = user_save($user, array("name" => $edit["name"], "mail" => $edit["mail"], "jabber" => $edit["jabber"], "theme" => $edit["theme"], "timezone" => $edit["timezone"], "homepage" => $edit["homepage"], "signature" => $edit["signature"])); /* ** Save new password (if required): */ if ($edit[pass1] && $edit[pass1] == $edit[pass2]) { $user = user_save($user, array("pass" => $edit[pass1])); } /* ** Redirect the user to his personal information page: */ drupal_goto("module.php?mod=user&op=view"); } } if ($error) { $output .= "". check_output($error) ."
"; } $output .= form_textfield(t("Username"), "name", $user->name, 30, 55, t("Your full name or your prefered username: only letters, numbers and spaces are allowed.")); $output .= form_textfield(t("E-mail address"), "mail", $user->mail, 30, 55, t("Insert a valid e-mail address. All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email.")); $output .= form_textfield(t("Jabber ID"), "jabber", $user->jabber, 30, 55, t("Insert a valid Jabber ID. If you are using your Jabber ID to log in, it must be correct. Your Jabber ID is not made public and is only used to log in or to authenticate for affilliate services.")); $output .= form_textfield(t("Homepage"), "homepage", $user->homepage, 30, 55, t("Optional") .". ". t("Make sure you enter a fully qualified URL: remember to include \"http://\".")); foreach ($themes as $key=>$value) $options .= "\n"; $output .= form_item(t("Theme"), "", t("Selecting a different theme will change the look and feel of the site.")); for ($zone = -43200; $zone <= 46800; $zone += 3600) $zones[$zone] = date("l, F dS, Y - h:i A", time() - date("Z") + $zone) ." (GMT ". $zone / 3600 .")"; $output .= form_select(t("Timezone"), "timezone", $user->timezone, $zones, t("Select what time you currently have and your timezone settings will be set appropriate.")); $output .= form_select(t("Language"), "language", $user->language, $languages, t("Selecting a different language will change the language of the site.")); $output .= form_textarea(t("Signature"), "signature", $user->signature, 70, 3, t("Your signature will be publicly displayed at the end of your comments.") ."". check_output($error) ."
"; } $output .= form_textfield("Username", "name", $edit["name"], 30, 55); $output .= form_textfield("E-mail address", "mail", $edit["mail"], 30, 55); $output .= form_textfield("Password", "pass", $edit["pass"], 30, 55); $output .= form_submit("Create account"); return form($output); } } function user_admin_access($edit = array()) { global $op, $id, $type; $output .= "e-mail rules :: username rulestype | mask | operations |
---|---|---|
allow | ". check_output($rule->mask) ." | aid\">delete rule |
deny | ". check_output($rule->mask) ." | aid\">delete rule |
%: matches any number of characters, even zero characters.
_: matches exactly one character.
". implode(" | ", array_keys($roles)) ." | |
---|---|---|
". check_output($perm) ." | "; foreach ($roles as $name => $value) { $output .= ""; } $output .= " |
name | operations |
---|---|
". check_output($role->name) ." | rid\">edit role |
username | last access | operations |
---|---|---|
". format_name($account) ." | ". format_date($account->timestamp, "small") ." | uid\">edit account |