Getting all Sentinel user related to roles - laravel

How can I get all users related to user roles by Sentinel? And how can I get all users in Sentinel? This does not work:
Sentinel::all()

You can try this to get all users related to a specific role.
$role = Sentinel::findRoleById(1);
// or findRoleBySlug('admin'); for example
$users = $role->users()->with('roles')->get();
and you can access the first user (and so on and so forth like this)
dd($users[0]['attributes']);
and to get all users you can do this:
$test = Sentinel::getUserRepository();
dd($test);

Related

How to create laravel cache facade based on user specific session

I have integrated redis cache driver in laravel. My app is social and music. So the authenticated user can access the app, I have to cache the data based on the logged in user. How can I implement this? Can anyone please help out?
Here is the code that I am using right now. Below is the code that fetches the logged in user's posts based on user id. In the code below, I am creating a cache tag tfeedsposts that will set the post data if the cache is not already set.
Is it the best practice to use the same cache tag for every logged in user?
Because I am thinking the same tag will not work for every logged in user, as the user will have different data, so storing the data in the same cache tag will not show the actual data to the user.
If there is an effective way to achieve this, then please let me know.
$cachename = 'tfeeds-offset:'.$this->offset.'-limit:'.$this->limit;
$cached = Cache::tags(['tfeedsposts'])->has($cachename);
if ($cached) {
$posts = Cache::tags(['tfeedsposts'])->get($cachename);
} else {
$posts = Post::whereRaw("visibility_id = 1 and user_id IN($friendsids_string) or visibility_id = 2 and user_id IN($follwingids_string) or visibility_id = 3 and user_id IN($follwingids_string) or visibility_id = 3 and user_id IN($friendsids_string)")->orWhere('user_id', $id)->with(['users_liked','users_viewed','shares', 'user','parentpost', 'comments','comments.user', 'comments.comments_liked', 'comments.replies', 'comments.replies.user', 'comments.replies.comments_liked','users_tagged' , 'notifications_user'])->latest()->paginate(Setting::get('items_page'));
Cache::tags(['tfeedsposts'])->put($cachename, $posts, $this->cachettl);
}

How to get a list of roles with certain permission in Spatie/permission (Laravel)

I am trying to get it by same way i am getting the list of users with a certain Permission, but with role it's not working throwing me
Call to undefined method Spatie\Permission\Models\Role::roles()
The way i get list of users with a certain role:
$permission = $request->permission;
$usersWithPerms = User::permission($permission)->get();
return array("usersWithPerms"=>$usersWithPerms);
The way im trying to get roles with a certain permission:
$groupsWithPerms = Role::permission('perms_givePermToRole')->get();
return array("groupsWithPerms"=>$groupsWithPerms);
BadMethodCallException
Call to undefined method Spatie\Permission\Models\Role::roles()
get the permissions of a role
and get the roles of a permission
$permission = Permission::findOrFail(1);
$groupsWithPerms = $permission->getRoleNames();
//dd($groupsWithPerms);
$role = Role::findOrFail(1);
$groupsWithRoles = $role->getPermissionNames();
//dd($groupsWithRoles);
The only thing i get its a name but not id of the roles with a certain permission:
$permission = Permission::findOrFail($request->idPermission);
$groupsWithPerms = $permission->getRoleNames();
Where: getRoleNames() is a method from spatie package.
So this works fine but you only get the names of the roles not ids.
There is no in-built shortcut to get them in the package, but querying for them is simple enough:
//Eager load roles to get all data in just 1 query
$permission = Permission::with('roles')
->where('name', 'perms_givePermToRole')
->first();
$rolesWithPerm = $permission->roles;
//or
$rolesWithPerm = Role::whereHas('permissions', function($permissions){
$permissions->where('name', 'perms_givePermToRole');
})->get();

how to get data from this result of query in laravel

I have written this query and i want access my user email.
The query is
$records = $this->projectRepository->with(['customer','provider'])->all();
dd($records);
foreach($records as $record) {
dd($record->provider->User->email);
}
the result of my dd($records) is as given below.i want to access highligted part.So let me know the for each loop.Whats mistake in my for each loop.I
enter image description here
Your provider relation returns an instance of your User model. So your provider and User are one and the same here. You can access the email attribute (or any other User attribute) like this:
$records = $this->projectRepository
->with(['customer','provider'])
->all();
foreach($records as $record):
// The user email
echo $record->provider->email;
// The user name
echo $record->provider->name;
endforeach
You have to give us more to work with next time. Give us an error. Give us your models. Give us something!
Now, from what I see, you need to remove the User, as the provider is of class User according to your provided dd screenshot. Thus, $record->provider->email should do the trick. Let me know if this works.

Laravel 4 - Reuse controllers and views using routes

I'm building a blog application with Laravel 4 which has two types of users, the administrator and the regular user.
When users create their accounts or when they log in all of their routes have the "user" prefix , like this:
GET user/posts = PostsController#index
For administrators all of their routes have the "admin" prefix , like so:
GET admin/users = UsersController#index
To view all the posts from a user as an administrator I do this:
GET admin/users/($id)/posts = PostsController#index
So if a regular user is logged then I get his ID from session, but if the user is an administrator then I get the regular user ID from the url.
I also have to update the links inside the views, like this:
As a regular user: link_to("user/posts/create", "Create Post")
As an administrator: link_to("admin/users/1/posts/create", "Create Post")
Is there a better approach this?
Yeah, all users are users.
Anything that requires you to be an administrator should have a filter for that on the route.
So for example, if an administrator was only allowed to go to user/posts/delete/{id}, that route would look something like...
Route::post('user/posts/delete/{id}', array('before' => 'adminFilter', 'uses' => 'PostController#deletePost');
I'm not sure how you figure out if a user is an admin or regular user, but your filter could look something like...
Route::filter('adminFilter', function()
{
if (!Auth::user()->isAdmin())
{
return Redirect::to('home')->with('message', 'You aren't an admin!');
}
});
And you would simply put an isAdmin() function in your User model which describes how to tell if a user is admin and returns true or false.

Magento: get roles and users

How can I get collection of all roles (System->Permission->Roles) and users which has this role?
Thanks.
To get all the Roles
$roles = Mage::getModel('admin/roles')->getCollection();
foreach($roles as $role):
echo '<br/>Role : '.$role->getId()." | ".$role->getRoleName();
endforeach;
To get the Role users
$roles_users = Mage::getResourceModel('admin/roles_user_collection');
foreach($roles_users as $roleuser):
$user = Mage::getModel('admin/user')->load($roleuser->getUserId());
echo '<br/>User : '.$user->getUsername()." | ".$user->getFirstname();
endforeach;
You can also get all role user ids with this
$roles = Mage::getModel('admin/roles')->load($roleId)->getRoleUsers();
I came across this post when I looked for a way to select all admin users belonging to a specific user role. (See this answer and this question.) The question here could be read as if Alex wants the admin users for each role. Since the answer does not sort by admin role, I would like to offer the following solution:
$usersByRole = array();
$adminRoles = Mage::getModel('admin/roles')->getCollection();
foreach($adminRoles as $adminRole) {
$adminRoleId = $adminRole->getId();
$adminRoleUserCollection = Mage::getModel('admin/role')->getCollection()
->addFieldToFilter('parent_id', ['eq'=>$adminRoleId])
->join(['user' => 'admin/user'], 'user.user_id=main_table.user_id');
$usersByRole[$adminRole->getRoleName()] = $adminRoleUserCollection;
}

Resources