How can I grant a specific role In Ubercart? - php

I'm trying to grant a specific role to users that order an amount equal or greater than 100.00 €: Conditional Actions is going really near the achievement, but I'm failing on the action (PHP required).
How can I grant a role using a PHP action in Ubercart Conditional Actions?

Based on a couple of related threads, I think you want to add an "Execute custom PHP code" action along the lines of the following (substituting in the appropriate role name in line #3):
if($account) {
$uid = $account->uid;
$role_name = 'YOUR SPECIFIC ROLE NAME GOES HERE';
$rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role_name));
// Load user object
$account = user_load(array('uid' => 1));
// Save the user object with the new roles.
if ($account !== FALSE) {
$roles = $account->roles + array($rid => $role_name);
user_save($account, array('roles' => $roles));
watchdog('user', 'uc ca added role to Ubercart created user');

Code provided needs to close two brackets. I dropped the if statements, grabbed the uid from the order and fixed the uid setting (it was '1'):
$uid = $order->uid;
$role_name = 'customer';
$rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role_name));
// Load user object
$account = user_load(array('uid' => $uid));
// Save the user object with the new roles.
$roles = $account->roles + array($rid => $role_name);
user_save($account, array('roles' => $roles));
watchdog('user', 'uc ca added role to Ubercart created user');

Related

Magento - Programatically retrieve and set admin users status

For a custom extension I'm building I need to be able to set and retrieve the status of an administrator user (not a customer) in Magento. I imagine you could achieve this like so;
$id; // ID of user stored here
$user = $mage->getadminuser($id); // store the user as an object or array in a variable with ID
$user->getStatus(); // return either true or false?
$user->setStatus(active or not active); // activate or deactivate the user
If anyone could provide me with the code to do this or documentation where I can find this easily?
Thanks!
$id = 5;
$admin = Mage::getModel('admin/user')->load($id);
if ($admin->getId()){
$admin->setIsActive(1);//or 0
$admin->save();
}

Please help me guess this hybridAuth code

Can somebody help me guess out this code..this is just a snippet and I think I included all the codes needed for my question. Actually this code is from hybridAuth. My question is, where does "user_id" from the last line came from? I wanted to know because $_SESSION["user"] gives the value of the "id". And I wanted to make another $_SESSION[" "] where I can place the value of email-add from the database (same location where that user_id's "id" exist)
// create an instance for Hybridauth with the configuration file path as parameter
$hybridauth = new Hybrid_Auth( $hybridauth_config );
// try to authenticate the selected $provider
$adapter = $hybridauth->authenticate( $provider );
// grab the user profile
$user_profile = $adapter->getUserProfile();
// load user and authentication models, we will need them...
$authentication = $this->loadModel( "authentication" );
$user = $this->loadModel( "user" );
# 1 - check if user already have authenticated using this provider before
$authentication_info = $authentication->find_by_provider_uid( $provider, $user_profile->identifier );
# 2 - if authentication exists in the database, then we set the user as connected and redirect him to his profile page
if( $authentication_info ){
// 2.1 - store user_id in session
$_SESSION["user"] = $authentication_info["user_id"];
The call to $authentication->find_by_provider_uid() returns an associative array, one key of which is user_id.
To see what other columns are returned by that call:
var_dump($authentication_info);
If the email is among the keys in that array, you may then set it in $_SESSION:
// Store the email into session if it is present in $authentication_info
// Use whatever the appropriate key you find, be it email, email_address, user_email, whatever...
$_SESSION['user_email'] = $authentication_info['email'];

Retrieve the group id of user in Ion Auth - Codeigniter

I am using Ion Auth for my codeigniter application and all seems good except for one thing.
I need to display a list of users along with the groups they are in. How can I retrieve the group id of a particular user without making my own models and querying for it.
$this->ion_auth->user($id)->row(); does not retrieve the group id.
Ion Auth has updated and removed the get_user function in the latest version. As a result of this, the below should return the current group id of the logged in user:
$this->ion_auth->get_users_groups()->row()->id
If you're wanting to get the group id of a particular user, you can pass the user id into the get_users_groups method.
get the Object:
$user_groups = $this->ion_auth->get_users_groups($user->id)->result();
Get the name of the group:
$this->ion_auth->get_users_groups($data['id'])->row()->name;
In my case:
$data['groups'] = $this->ion_auth->get_users_groups($data['id'])->row()
You can check the offcial doc about get users groups:
http://benedmunds.com/ion_auth/#get_users_groups
I came across this because I wanted to add a user's group_id(s) to the session upon successful login. In case anyone is interested, here's how I did it (once i managed to figure out where everything was being done).
In ion_auth_model.php, I took the set_session function:
public function set_session($user)
{
$this->trigger_events('pre_set_session');
$session_data = array(
'identity' => $user->{$this->identity_column},
'username' => $user->username,
'email' => $user->email,
'user_id' => $user->id, //everyone likes to overwrite id so we'll use user_id
'old_last_login' => $user->last_login
);
$this->session->set_userdata($session_data);
$this->trigger_events('post_set_session');
return TRUE;
}
and modified it to this:
public function set_session($user)
{
$this->trigger_events('pre_set_session');
$session_data = array(
'identity' => $user->{$this->identity_column},
'username' => $user->username,
'email' => $user->email,
'user_id' => $user->id, //everyone likes to overwrite id so we'll use user_id
'old_last_login' => $user->last_login
);
//get user group ids for user and pass to session
$groups = $this->ion_auth->get_users_groups($user->id)->result();
foreach ($groups as $row){
$session_data['groups'][] = $row->id;
}
$this->session->set_userdata($session_data);
$this->trigger_events('post_set_session');
return TRUE;
}
now it writes an array to the session called groups that is a list of all the groups the user belongs to.
The only other thing I had to do was to modify the logout function in Ion_auth.php (in application/libraries) to make sure the group session variable is unset by adding
$this->session->unset_userdata('groups');
to the list of other unset_userdata() statements.
I know I probably should have just extended the libraries/models to keep the core untouched, but you could take what I did and easily do that.
hope this helps someone.
Rob
Try adding this to ion_auth_model.php
(Or place the query somewhere else)
/**
* get_all_users_with_group
*
* #return array
**/
public function get_all_users_with_group()
{
$this->trigger_events('get_all_users_with_groups');
return $this->db->select(
$this->tables['users'].'.*, '.
$this->tables['users_groups'].'.'.$this->join['groups'].' as group_id, '.
$this->tables['groups'].'.name as group_name, '.
$this->tables['groups'].'.description as group_desc'
)
->join($this->tables['users_groups'], $this->tables['users_groups'].'.'.$this->join['users'].'='.$this->tables['users'].'.id')
->join($this->tables['groups'], $this->tables['users_groups'].'.'.$this->join['groups'].'='.$this->tables['groups'].'.id')
->get($this->tables['users']);
}
use in the controller:
$groups = array(1,2,3,4);
$this->data['list_staff'] = $this->ion_auth->users($groups)->result(); // выбираем всех teachers из зарегистрированных пользователей
foreach ($this->data['list_staff'] as $k => $one_staff)
{
$this->data['list_staff'][$k]->groups = $this->ion_auth->get_users_groups($one_staff->id)->result();
}
and in view use:
<?php foreach($list_staff as $one):?>
<?php foreach ($one->groups as $group):?>
<?php echo $group->description;?>,
<?php endforeach?>
<?endforeach;?>
but what about query to get groups for current logged in user (for example in my profile) I find next decision.
Controller:
$this->data['this_user_groups'] = $this->ion_auth->get_users_groups()->result();
and view just:
<?php foreach ($this_user_groups as $group):?>
<?php echo $group->description;?>,
<?php endforeach?>

Multi-level Registration in Drupal

How to implement two type of registration like student and teacher?
I need two type of registration one for Teacher and one for Student. Both are different registration and both have different roles. Is it possible in Drupal? And also I need registering Student there is no admin approval but for Teacher registration, admin approval is required. How can I achieve this in Drupal 6?
In the custom user registration form add one more select box field with ROLE TYPE (student, teacher).Then on the submit hook check like shown below.
function add_student_form_submit($form, &$form_state) {
$fields = array();
$fields['is_new'] = true;
$fields['name'] = $form_state['values']['user_name'];
$fields['pass'] = $form_state['values']['pass'];
$role_type = $form_state['values']['role_type'];
//Add the user to the corresponding role
$fields['roles'] = array($role_type)
//here you can achieve the thing which you want.If the role is a teacher then set
//status = 0, else status = 1
if($role_type == 'student')
$fields['status'] = 1;
else
$fields['status'] = 0; // $user = user_save(drupal_anonymous_user(), $fields); //This works in D7
$user = user_save('', $fields); //pretty sure this is what works in D6 }
If the user is the teacher you should go to http://localhost/domain_name/admin/user/user. Here you can filter the Inactive users and activate them.
hey you can use below modules for creating multilevel registration.
http://drupal.org/project/content_profile
http://drupal.org/project/autoassignrole
above modules help you to create multilevel registration form in site. from content profile you can create form and also give auto assign role to student.
As far as i'm aware, drupal doesn't provide any mechanism for having multiple types of registration forms. However you can fairly easily create your own registration form from scratch. All you really need is the user_save function to create a new user. See the sample code below as part of a form_submit hook
function add_student_form_submit($form, &$form_state) {
$fields = array();
$fields['is_new'] = true;
$fields['name'] = $form_state['values']['user_name'];
$fields['pass'] = $form_state['values']['pass'];
$fields['status'] = 1;
// $user = user_save(drupal_anonymous_user(), $fields); //This works in D7
$user = user_save('', $fields); //pretty sure this is what works in D6
}
Using this you can create whatever custom logic you want for each form

Custom user-roles and -permissions in installation script

Is it possible to write some code in your .install-file of your D7 website which allows you to generate user roles and permissions automatically? I always though so, but right now, I can't think of a way to do it.
Any advice?
Absolutely:
function mymodule_install() {
// Make the new role
$role = new stdClass;
$role->name = 'new role name';
$role->weight = 3;
user_role_save($role);
// Permissions to assign to the role.
// Note these are defined in hook_permission()
$perms = array(
'access administration pages',
'view content',
'any other permission you want'
);
// Grant the permissions. This function takes care of all necessary cache resets
user_role_grant_permissions($role->rid, $perms);
}

Categories