本文介绍了如何使用PHP在Drupal中分配角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要为通过PHP购买等于或大于100欧元的会员分配一种溢价状态。I need to assign a kind of "premium status" to members that purchase an amount equal or greater than 100 € via PHP.条件操作已设置(user = anonymous / authenticated AND total amount =等于/大于100 AND user =!premium),但是我错过了PHP部分,实际上说然后授予他高级会员资格。 Conditional Actions are already set up (user = anonymous/authenticated AND total amount = equal/greater than 100 AND user =! premium) but I'm missing the PHP part to actually say "then grant him the premium membership". 如何实现?编辑:以下代码是否正确? $ b is the below code correct?if ($account) { $uid = $account->uid; $role_name = 'authenticated user'; $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role_name)); db_query("INSERT INTO {users_roles} (uid, rid) VALUES(%d, %d)", $uid, $rid); watchdog('user', 'uc ca added role to Ubercart created user');} 推荐答案 a href =http://api.drupal.org/api/function/user_load/6 =noreferrer> user_load() 和 user_save() :You can do this with user_load() and user_save():$uid = 1; // UID of user to add role to$role_name = 'test role'; // Name of role to add// Get RID of role$rid = db_result(db_query("SELECT r.rid FROM {role} r WHERE r.name = '%s'", $role_name));// Load user object$account = user_load(array('uid' => 1));// Save the user object with the new roles.if ($account !== FALSE && !isset($account->roles[$rid])) { $roles = $account->roles + array($rid => $role_name); user_save($account, array('roles' => $roles));}如果您想为多个用户批量进行此操作,则 user_multiple_role_edit() :If you wanted to do this in bulk for multiple users, there's user_multiple_role_edit():$uids = array(1, 2, 3, 4); // UIDs of users to add role to$role_name = 'test role'; // Name of role to add// Get RID of role$rid = db_result(db_query("SELECT r.rid FROM {role} r WHERE r.name = '%s'", $role_name));// Add the role to UIDsuser_multiple_role_edit($uids, 'add_role', $rid); 修改 如果您想为当前用户执行此操作(例如您在评论中提到的支票的一部分),您可以执行以下操作:EditIf you wanted to do this for the current user (like part of the check you mentioned in a comment), you can do:// Check for value over 100.00if ($total_value > 100) { global $user; // Retrieve user object for currently logged in user. $role_name = 'test role'; // Name of role to add // Get RID of role $rid = db_result(db_query("SELECT r.rid FROM {role} r WHERE r.name = '%s'", $role_name)); // Save the user object with the new role. if (!isset($user->roles[$rid])) { $roles = $user->roles + array($rid => $role_name); user_save($user, array('roles' => $roles)); }}// Other code here. 这篇关于如何使用PHP在Drupal中分配角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 06:32