I am trying to get the name of the role of the currently logged in admin. I can get the admin user, but I can't figure out how to query their role name. The Magento docs are weak =/
$usr = Mage::getSingleton('admin/session')->getUser();
Ideas anyone?
Spoke too soon... I got the role name as follows:
$roleId = implode('', Mage::getSingleton('admin/session')->getUser()->getRoles());
$roleName = Mage::getModel('admin/roles')->load($roleId)->getRoleName();
Using this code you will get the role of current user
$admin_user_session = Mage::getSingleton('admin/session');
$adminuserId = $admin_user_session->getUser()->getUserId();
$role_data = Mage::getModel('admin/user')->load($adminuserId)->getRole()->getData();
$role_name = $role_data['role_name'];
Mage::getSingleton('admin/session')->getUser()->getRole()->getRoleName();
M.
Here's another one that may be a little friendlier:
$acl = Mage::getResourceModel('admin/acl')->loadAcl();
$acl->isAllowed($user->getAclRole(), 'admin/foo/bar'));
That will return a boolean. $user is an admin/user object.
Related
I have been changing user roles name on wordpress how ever
If i call
implode(', ', $user->roles)
I still get the original role names, Administrator, Subscriber, etc.
So, i figured something like this would be the correct way.
if(implode(', ', $user->roles) = "administrator"){
$role = "Site owner";
}
else{
$role = "User";
}
echo $role;
I am running this for each user,how ever this does not work.
What should i be doing to change the names? the backend presents the name correctly. how ever i wish to have the names on the front end for the users accounts.
More specifically, a user's role can be set by creating an instance of the WP_user class, and calling the add_role() or remove_role() methods.
Example
Change a subscribers role to editor
// NOTE: Of course change 3 to the appropriate user ID
$u = new WP_User( 3 );
// Remove role
$u->remove_role( 'subscriber' );
// Add role
$u->add_role( 'editor' );
Hopefully that's more helpful than my initial response, which wasn't necessarily as helpful.
I'm trying save user IP adress after login on website. I'm using laravel 5.2 framework. I got user table and login_ip row. My code looks like that:
$user = User::where('login_ip', Request::getClientIp());
$user->save();
But it does not saving. What i'm doing wrong? Sorry for my bad english :)
If you want to save IP for current user, you can do this:
auth()->user()->update(['login_ip' => Request::getClientIp()]);
This will not create additional query as code in shoieb0101, Amit and Ronald answers.
Don't forget to add login_ip to the $fillable array in User model:
protected $fillable = ['login_ip'];
If you want to save IP for only logged in users and ignore guests, you can do a check:
!auth()->check() ? : auth()->user()->update(['login_ip' => Request::getClientIp()]);
Try
$user = User::find(auth()->user()->id);
$user->login_ip = Request::getClientIp();
$user->save();
//assuming $userid is also requested
$user = User::where('id', $userid);
$user->login_ip = Request::getClientIp();
$user->save();
You can try it as:
auth()->user()->login_ip = Request::getClientIp();
auth()->user()->save();
OR
auth()->user()->save(['login_ip' => Request::getClientIp()]);
Note - It will update the user's login_ip in single query.
You dont have to get logged user form db, all info about your user you have in Auth::user() so:
Auth::user()->login_ip = Request::getClientIp();
Auth::user()->save();
or
Auth::user()->login_ip = $request->ip();
Auth::user()->save();
but you need to have Request $request as parameter of your method.
I am probably stating the obvious, but your first line...
$user = User::where('login_ip', Request::getClientIp());
... returns an Eloquent query builder, right?
So, a save() on this will never work?
$user = User::where('login_ip', Request::getClientIp())->first();
... will return an actual User (if in the DB), which makes save() also possible.
Or did you make a typo in your OP?
What I'm trying to do seems like it should be simple, but I can't figure it out. All I really want is a simple way to add one model to the database that belongs to another model. I know it's confusing wording so I'll illustrate.
I have a User model and an Address model. I have a user defined with an id of 1 and I want to add an address to that user. I can add the Address like this:
$a = new App\Address;
$a->user_id = 1;
$a->line_1 = '1234 Something Rd.';
$a->city = 'Fort Worth';
$a->state = 'TX';
...
But it would be so much easier if I could just say:
$u = App\User::first();
$u->address->create([array of options]); /* Or something */
So my questions are:
Is this possible?
How can I do it?
What is this called? Does it have a name?
Thanks!
$u = App\User::first();
App\Address::create(['user_id'=>$u->id, etc]);
or
$address = App\Address::create([options]);
$u->address()->save($address);
Do not forget to set Model Address.php like this:
protected $fillable = ['user_id','etc'];
You cannot call
$u->address->create
because as User does not yet have a address model, the method will return NULL, so ->create will fail.
Yes, you can Insert Related Models:
$user = App\User::first();
$address = App\Address::create($attributes);
$user->address()->associate($address);
Note that how you do this depends on the type of relationship.
I'm trying to get a logged in users type (eg super admin, registered). I've tried this code:
$user =& JFactory::getUser();
$curref = $user->usertype();
Which gives a function not found error. What is the correct way to get the user type name, without a db query if possible.
You just need to treat usertype as a member, not a method.
$type = $user->usertype;
Documentation: http://docs.joomla.org/Accessing_the_current_user_object
You can take a look at the $user object structure by doing a var_dump. Try this, and inspect the output:
var_dump( $user );
So if you want to iterate over the groups array, you could do the following:
$groupIDs = array();
foreach( $user->groups as $groupID ){
$groupIDs[] = $groupID;
}
var_dump( $groupIDs );
You can also use some joomla methods to return the groups in different ways. You may want to check out this forum thread: http://forum.joomla.org/viewtopic.php?t=530721
I was creating its own login system, but all recommmended sfDoctrineGuardPlugin. unfortunately I dont understand this...
I go to http://localhost/frontend_dev.php/login and i logged in. i am in class sfGuardSecurityUser and method public function signIn($user, $remember = false, $con = null)
where there did variable $user?
there is:
$this->setAttribute('user_id', $user->getId(), 'sfGuardSecurityUser');
but if i use:
$this->getUser()->getAttribute('user_id');
in own module then it is NULL. Works only $this->getUser(); but this is only Name and login. How can i get ID logged user?
I also added in table sf_guard_user_permission user_id 2 permission_id 2 and how can i check this in other module?
Thanks for help!
This is simple, in an action for exemple :
$this->getUser()->getGuardUser()->getId();