<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 431,  comments - 344,  trackbacks - 0
    1.Introduction to hook_user()

    Implementing hook_user() gives your modules a chance to react to the different operations performed on a user account, and to modify the $user object. Let’s examine the function signature:

    function hook_user($op, &$edit, &$user, $category = NULL)

    The $op parameter is used to describe the current operation being performed on the user account and can have many different values:

    • after_update: Called after the $user object has been saved to the database.

    • categories: Returns an array of categories that appear as Drupal menu local tasks when the user edits the user account. See profile_user() in profile.module for an implementation.

    • delete: A user has just been deleted from the database. This is an opportunity for the module to remove information related to the user from the database.

    • form: Inject an additional form field element into the user edit form being displayed.

    • insert: The new user account is about to be created and inserted into the database.

    • login: The user has successfully logged in.

    • logout: The user just logged out and his or her session has been destroyed.

    • load: The user account was successfully loaded. The module may add additional information into the $user object.

    • register: The user account registration form is about to be displayed. The module may add additional form elements to the form.

    • submit: The user edit form has been submitted. Modify the account information before it is sent to user_save().

    • update: The existing user account is about to be saved to the database.

    • validate: The user account has been modified. The module should validate its custom

    data and raise any necessary errors.

    • view: The user’s account information is being displayed. The module should return

    its custom additions to the display as an array. The view operation ultimately calls

    theme_user_profile to format the user profile page. More details on this shortly.

    The $edit parameter is an array of the form values submitted when a user account is being created or updated. Notice that it’s passed by reference, so any changes you make will actually change the form values.

    The $user object is also passed by reference, so any changes you make will actually change the $user information.

    The $category parameter is the active user account category being edited.

    Caution Don’t confuse the $user parameter within hook_user() with the global $user object. The $user parameter is the user object for the account currently being manipulated. The global $user object is the user currently logged in.

    2.The User Registration Process

    Add a legalagree.module

    <?php

    function legalagree_user($op, &$edit, &$user, $category = NULL) {

           switch ($op) {

                  // User is registering

                  case 'register':

                         //Add a fieldset containing radio buttons to the user registration form

                         $fields['legal_agreement'] = array(

                                '#type' => 'fieldset',

                                '#title' => t('Legal Agreement')

                         );

                         $fields['legal_agreement']['decision'] = array(

                                '#type' => 'radios',

                                '#options' => array(t('I disagree'), t('I agree')),

                                '#default_value' => 0,

                                '#description' => t('By registering at %site-name, you agree that

                                at any time, we (or our surly, brutish henchmen) may enter your place of

                                residence and smash your belongings with a ball-peen hammer.',

                                array('%site-name' => variable_get('site_name', 'drupal')))

                         );

                         return $fields;

                  case 'validate':

                         // Make sure the user selected radio button 1 ('I agree').

                         // the validate op is reused when a user updates information on

                         // The 'my account' page, so we use isset() to test whether we are

                         // on the registration page where the decision field is present.

                         if (isset($edit['decision']) && $edit['decision'] != '1') {

                                form_set_error('decision', t('You must agree to the legal agreement before

                                registration can be completed.'));

                         }

                         return;

                  case 'insert':

                         // Record information for future lawsuit.

                         watchdog('user', t('User %user agreed to legal terms', array('%user' => $user->name)));

                         return;

           }

    }

    3.Adding Data to the $user Object

    Loginhistory.module

    <?php

    function loginhistory_user($op, &$edit, &$account, $category = NULL) {

           switch($op) {

                  case 'login':

                         // Record timestamp in database

                         db_query("INSERT INTO {login_history} (uid, timestamp) values (%d, %d)", $account->uid, $account->login);

                         break;

                  case 'load':

                         // Add the number of times user has logged in.

                         $account->loginhistory_count = db_result(db_query("SELECT COUNT(timestamp) as count FROM {login_history} WHERE uid = %d", $account->uid));

                         break;

                  case 'view':

                         // Add a field displaying number of logins.

                         $items['login_history'] = array(

                                'title' => t('Number of logins'),

                                'value' => $account->loginhistory_count,

                                'class' => 'member'

                         );

                         return array(t('History')=>$items);

           }

    }

    Loginhistory.install

    <?php

    function loginhistory_install() {

           switch ($GLOBALS['db_type']) {

                  case 'mysql':

                  case 'mysqli':

                         db_query("CREATE TABLE {login_history} (

                                uid int NOT NULL default '0',

                                timestamp int NOT NULL default '0',

                                KEY (uid)

                         )/*!40100 DEFAULT CHARACTER SET UTF8 */");

                         break;

                  case 'pgsql':

                         db_query("CREATE TABLE {login_history} (

                                uid int_unsigned default '0',

                                timestamp int_unsigned NOT NULL default '0',

                                KEY (uid)

                         )");

                         break;

           }

    }

    function loginhistory_uninstall() {

           db_query("DROP TABLE {login_history}");

    }

    4.Simple External Authentication

    Let’s implement a very simple external authentication module that might be used inside a company where simple usernames are used. Suppose your company only hires people named Dave, and usernames are assigned based on first and last names. This module authenticates anyone whose username begins with the string dave, so the users davebrown, davesmith, and davejones will all successfully log in.

    <?php

    /**

    * Implementation of hook_auth()

    */

    function authdave_auth($username, $pass, $server) {

           // Does username begin with 'dave'?

           if (substr(drupal_strtolower($username), 0, 4 ) == 'dave') {

                  // Make a global variable to note that we did the authentication.

                  global $authdave_authenticated;

                  $authdave_authenticated = TRUE;

                  return TRUE;

           }

           else {

                  return FALSE;

           }

    }

    If a row in the users table does not exist for this user, one will be created. However, no e-mail address has been provided at login like it was for Drupal’s default local user registration, so a module this simple is not a real solution if your site relies on sending e-mail to users. You’ll want to set the mail column of the users table so you will have an e-mail address associated with the user. To do this, you can have your module respond to the insert operation of the user hook, which is fired whenever a new user is inserted:

    /**

    * Implementation of hook_user()

    */

    function authdave_user($op, &$edit, &$account, $category = NULL) {

           switch($op) {

                  case 'insert':

                         // New user was just added; if we did authentication,

                         // look up email address of user in a legacy database.

                         global $authdave_authenticated;

                         if ($authdave_authenticated) {

                                $email = mycompany_email_lookup($account->name);

                                // Set email address in the user table for this user.

                                db_query("UPDATE {users} SET mail = '%s' WHERE uid = %d", $email,

                                $account->uid);

                         }

                         break;

           }

    }

    posted on 2007-12-03 14:32 周銳 閱讀(261) 評論(0)  編輯  收藏 所屬分類: PHP
    主站蜘蛛池模板: 中文字幕亚洲一区| 国产成人亚洲综合网站不卡| 亚洲Av无码专区国产乱码DVD | 97国免费在线视频| 日韩一卡2卡3卡4卡新区亚洲 | 久久精品无码专区免费东京热| 亚洲一区精品无码| 国产日韩一区二区三免费高清| 大学生a级毛片免费观看| 亚洲av一本岛在线播放| 成年人在线免费观看| 亚洲人成人网站18禁| 免费国内精品久久久久影院| 亚洲黑人嫩小videos| 亚洲无砖砖区免费| 亚洲三级在线观看| 日本一道在线日本一道高清不卡免费| 久久久久久亚洲精品无码| 亚洲精品成人网久久久久久| 国产亚洲精aa在线看| 成人毛片免费网站| 搜日本一区二区三区免费高清视频| 你是我的城池营垒免费观看完整版| 亚洲gv白嫩小受在线观看| 亚洲一级免费毛片| 日韩亚洲人成在线综合日本| 美美女高清毛片视频黄的一免费 | 中文字幕免费在线看线人动作大片| 永久免费av无码网站韩国毛片| 亚洲中文字幕一二三四区| 免费大香伊蕉在人线国产| 中文字幕免费在线看| 亚洲六月丁香六月婷婷色伊人| 免费污视频在线观看| 亚洲国产成人va在线观看网址| 国产美女被遭强高潮免费网站| 叮咚影视在线观看免费完整版| 国产成+人+综合+亚洲专| 精品女同一区二区三区免费站| 亚洲av无码一区二区三区在线播放| 久久久久亚洲AV成人网人人网站 |