Welcome Guest, Not a member yet? Register   Sign In
Datamapper User Can Have Multiple Roles, Issue.
#1

[eluser]Vheissu[/eluser]
I'm currently in the process of developing an auth library that uses Datamapper Overzealous Edition to allow for a pretty feature packed auth class. One of the primary features of the class is that a user can be assigned to multiple roles.

So a user can be assigned to an admin role, user role, guest role and a moderator role. Or any of those, or all at the same time (if that makes sense). I've created a join table, and created my models a user model and role model that both have a $has_many defined both ways.

My issue is that when adding a new user, how am I able to assign a user to multiple roles when saving a new user object? Do I build an array and then iterate over it saving multiple times? Would appreciate some help on this. See below for what I have already.

Code:
public function create_user($login, $password, $fields = array(), $roles = array())
    {
        $login_type = config_item('auth.login_type');
        
        if ($login_type == 'auto')
        {
            if (!valid_email(config_item('auth.login_type')))
            {
                $login_type = 'username';
            }
            else
            {
                $login_type = 'email';
            }  
        }
        
        $u = new User();
        $u->where($login_type, $login);
        $u->get();
        
        if ( !$u->exists() )
        {
            $u->clear();
            
            $salt     = $this->generate_random();
            $password = $this->create_password($this->generate_random(), $salt);
            
            $u->{$login_type} = $login;
            $u->salt          = $salt;
            $u->password      = $password;
            
            if (!empty($fields))
            {
                foreach ($fields AS $field => $value)
                {
                    $u->{$field} = $value;
                }
            }
            
            if (!empty($roles))
            {
                foreach ($roles AS $role)
                {
                    // How do I have x amount of roles to this user?
                }
            }
            
        }
        
    }
#2

[eluser]WanWizard[/eluser]
Code:
// create a user
$user = new User();
$user->name = 'john';

// get the roles
$roles = new Role();
$roles->where( "your_criteria_here" )->get();

// save the user, and link to the selected roles
$user->save($role->all);
#3

[eluser]Vheissu[/eluser]
So would I have for my roles clause, multiple where statements looping through my roles array supplied to the function?

For example;

An array of roles has been passed through to the function. This array has role ID's 2 and 4 in it. How do I query the roles table for both of these ID's so I can relate a user to both role ID's 2 and 4 without having to query or save multiple times?

I got it to work using the following code, but one of its caveats is that I can't check if the overall save result was successful due to the save taking place inside of a foreach loop and the user being saved beforehand.

Code:
// Save the user
            $u->save();
            
            if (!empty($roles))
            {
                foreach ($roles AS $role)
                {
                    $r = new Role();
                    $r->get_by_id($role);
                    $u->save($r);
                }
            }
#4

[eluser]WanWizard[/eluser]
Code:
// get roles by multiple ID's
$role->where_in('id', array(2,4))->get();

After this, your $role object contains all selected roles, and you can use the save() syntax I gave earlier to save the new user, and connect them to all selected roles in one go.
#5

[eluser]Vheissu[/eluser]
Haha, oh man. I cannot believe it was as simple as using where_in. Everything I read on Stackoverflow didn't mention using it and instead were using a bunch of foreach's which makes checking if something is completed harder and in my opinion would impact performance.

Thanks for your help WanWizard, I love your work man.
#6

[eluser]WanWizard[/eluser]
Thanks for the kudos, that makes all the long nights worthwhile... :exclaim:




Theme © iAndrew 2016 - Forum software by © MyBB