User Management / Get iduser - php

I'm creating a user management where you can change a user's role, USER or ADMIN.
I'm a beginner in Laravel 9, and I would like to know how can I get the iduser when I click on the user's button ? For example this is a user profile.
The button can change his role form user to admin but I can't get his id...
I tried this but it's written 0 instead of 2 (his iduser)
$userID = User::find('iduser');
dd($userID);
Thank you very much!
Edit : When I click on a user profile, I want to get HIS/HER id, not mine or logged user :)
FOR EXAMPLE :
This user, I click on "Changer en administrateur" (change to admin), I just want his id ;)

If you want to get logged in user id you can do
$userID = Auth::user()->id;
Either from User model ,try
$userID = User::all()->pluck('id');

$request->validate([
'type' => 'required',
]);
DB::table('users')
->where('iduser', $request->iduser)
->update(
['type' => $request->type]
);
This one works to select a user's id :)

Related

How to make user login as role features?

So currently I'm developing web apps that will have users management and role management (roles using spatie/laravel-permission). it have a lot of roles for example:
Root (super admin)
Admin Daerah(based on the village)
admin unit (based on his working place/unit)
Kepala Badan
Kepala seksi
Staff
Citizen
Etc.
Every user can (and will) have more than one roles for example user with roles ['admin daerah', 'staff', and 'citizen'].
I want to make a feature where every time a user login and has more than one role, he will be redirected to a page that shows his role lists and he will select one of the roles. after he selects the role, it will be redirected to the dashboard and only have permission based on the selected role (for example he select 'Staff' role, so he only can do 'things' that be allowed as staff).
I already find on StackOverflow and asking on another forum, but I still can't find my solutions, and I'm still new on laravel too. I hope someone can give some solution and the logic of how to do it. thank you
Edit 1: because I'm using spatie/laravel-permission, its already have model_has_roles table as the pivot.. so it will contain role_id, role_name, guard_name, model_id as user_id, foreign key unit_id, foreign key institution_id, is_active
Check this way:
Create role_ids(text) row in users table. Save user roles in this row via implode() method. (example: 1,4,3,6)
When user login, check this role_ids, if there is more than 1 role ask for choosing one of them.
After choosing, update user role in spatie.
But don't forget, when you will update user role via Administration Panel you should update it not in spatie roles table, but in role_ids in users table. otherwise user can get access to other roles if user already logged in.
You can try an approach like this:
Once user has logged in, include the query below before redirecting the user to the intended view.
Ex.
$userRoles = ModelHasRole::where('user_id', '=', Auth::User()->id)->get();
return view ('select_role_page', compact('userRoles'))
Then in your select_role_page blade file
#foreach ($userRoles as $indiv_userRole)
#if ($indiv_userRole->role_name == 'admin')
Link to Admin Dashboard
#endif
#if ($indiv_userRole->role_name == 'staff')
Link to staff Dashboard
#endif
#if ($indiv_userRole->role_name == 'citizen')
Link to Citizen Dashboard
#endif
#endforeach
If the user has any of the roles indentified above, the appropriate link will be shown.
EDIT: Make sure you have a route for the links above like the one below.
Route::get('/role/{id}', 'RoleController#showDashboard');
And in your controller.
public function showDashboard ($id) {
if ($id == 'admin') {
return view ('admindashboard');
}
if ($id == 'staff') {
return view ('staffdashboard');
}
if ($id == 'citizen') {
return view ('citizendashboard');
}
}

How to give only one user more authorization than other users?

I have a question about giving authorization to a user by Admin but not to all users.
Example: Let assume we have 3 levels of users:
1) Admin as genera manager
2) HR , Sales as department head
3) users as normal employees
let say the Admin wants to go for a vacation for one week and wants to give HR department head an authorization(s) during the vacation but not Sales department head.
I DONT know if there is a key word in PHP for this scenario.
In my idea I want to make a php page in Admin account that shows all employees and each employee has a button to go to next page to show all authorization that the Admin can do it and in each one has check box. If any check box is selected, the auth. will be shown in that user.
I hope this is help.
Thank you
i think you should add a field in your user_tbl roleint and its value will be 0 for user 1 for manager and 2 for Admin
when any user login then you have to check the role of the user and on its behalf (like if user A is login and its role is 2 then ) you can show admin features for him...

How to retrieve only those records that do not have an associated record?

I am using CakePhp 3.x and i am trying to filter data on the bases of associated data.
Let suppose i have two models Users and Profiles
Users Has-one Profile and Profiles belongs to a user.
so i want to get those users who haven't profile so kindly help me how can i apply the conditions ?
$this->Users->find('all',[
'conditions'=>['Profile.id'=>Null]
])
I was trying like this i thing it is wrong and then i tried it with contain but contain makes filter on associated data not to the users so is there any way to get those user who have no profile ?
See the Link below :-
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#using-notmatching
There are three type of filtration of data on the bases of associated data.
1 -> matching
2 -> noMatching
3 -> See the Link above
Matching
This function will filters results that have relation to the specified association: for now it's profiles.
$this->Users->find()->matching('Profiles') //This will get those user how have profile
NoMatching
The opposite of matching() is notMatching(). This function will change the query so that it filters results that have no relation to the specified association:
$this->Users->find()->noMatching('Profiles') //This will get those user how have no profile
You could try something like this:
$query = $this->Users->find()
->contain([
'Profiles' => function ($q) {
return $q
->select(['id', 'name']) // Your profile fields, say id, name etc.
->where(['Profiles.id' => NULL]);
}
]);
Hope this helps.

How to input condition in yii find()?

I have two tables Account and User.
Account table contains account_id, username, password
while User table contains user_id, account_id, first_name, last_name.
What I want is for yii to display the contents of the user table of the one logged in so I used this code
$user= Yii::app()->user->id; //to get the account_id of user logged in
$userModel = User::model()->find(array('condition'=>'account_id' == $user));
//to find data in the user table with the account_id similar to the
//account_id of the one logged in
print_r($userModel); //to check if I got the correct data
but for some reason, no matter who logs in, the print_r($userModel) is returning data with the account_id == 1
please help :/
If you are using Yii 1 you can try with findByAttribute
$userModel = User::model()->findByAttributes(array('account_id'=>$user));

CakePHP: How to build a User's Profile page

How do we build a profile page that outputs the user's data? and this page can only be viewed by the user who logged in. It's something like when we go to our profile page and view our own username, password, email, address..etc. Then we may edit it by ourselves. It, of course can't be edited by other users.
I'm confused with the need of a profile table, now I think we would not need it? we can just populate the data using some PHP logic on a page we create as profile.ctp ?
This is confusing, I followed this http://book.cakephp.org/#!/view/1041/hasOne and created a profile table with some fields my users table has, and then with a foreign key called user_id. I checked on User and Profile model both are correctly defined in the relationship. I have this in Profile model:
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
and this in User model:
var $hasOne = 'Profile';
As I browse to my profile/index there are field names without any records. It's empty set. I thought it was supposed to retrieve data from the users' table ??
What's the best way to create a profile page for the existing users.. and the upcoming registrations ?
You do not need a profile table (if you already have a users table with their info).
One way of having this done is, after user validation, compare his id with id of the user which profile he wants to visualize. If those match, then it's a user who is viewing his own profile, and you can let him view his info.
Ofcourse there's a great deal of security issues you would have to take into account if you are thinking to make this a public accessible web site.

Categories