Trying to display a user with the specific role in cakephp 2x - php

I am trying to display table Users, and I want it to display the user with the role "Moderator" only.
public function moderators() {
$this->set('users', $this->paginate());
$this->User->find('all', array(
'conditions' => array('User.role' => 'moderator')
));
This is my controller, and it seems that it is still displaying all data in my table.

Try to use custom query for that:
$this->User->query("select * from users where role = 'moderator';");

Related

Display data in gridview base on rbac role in yii2

How to display records in gridview in yii2 using rule created for roles, in RBAC ?
Suppose, there is two roles "admin" and "agent".
Now the requirement is;
In grid for agent, display only client which is assigned to that
agent.
For admin, grid will show all client list.
Here the example I am using this in my code
// User.php -> Model
public function getUserRolesAsArray($userId)
{
$roles = Yii::$app->authManager->getRolesByUser($userId);
if (!empty($roles)) {
foreach ($roles as $role) {
$userRole[] = $role->name;
}
return $userRole;
}
}
// view.php -> view file
[
'label' => 'Role',
'value' => $model->getUserRoles($model->id) ?? null,
],
Kindly try this i think this may be help you
It is done,
I have to create a permissions that will given to role, and based on that permission, DataProvider query is modified

CakePHP User Role Retrieval

Im trying to pass a condition to my UserController in order to view certain Users whose Role matches to the page I'm trying to access.
For example:
/admin => User.admin
/student => User.student
I'm able to retrieve a list of all registered students using the find('all') function, but when I try to filter roles through setting conditions I'm getting an error that references my code and the role I'm trying to retrieve.
Here's my function in my controller:
public function index($role="admin") {
//debug($role);
$this->User->recursive = 0;
$this->request->data('users', $this->User->find('all',
array('conditions' => 'User.role')));
$this->set('users', $this->paginate());
}
Any tips?
conditions should be an array of conditions.
i.e. array('role' => 'role_value')
See documentation at http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
For adding conditions, use WHERE option with find().
Like:
public function index($role="admin") {
$this->User->recursive = 0;
$this->request->data('users',
$this->User->find()->where(['conditions' => 'User.role']));
$this->set('users', $this->paginate());
}
OR
If role is the DB table field name, the code is simple:
public function index($role="admin") {
$this->User->recursive = 0;
$this->request->data('users',
$this->User->findByRole());
$this->set('users', $this->paginate());
}

Showing a list of records which contains similar values in one column

I have a table named 'Tbluser'. It contains 'company_code, user_code'.I can show this table using grid view.
i want to show only one specific company users. example, if a company_code: 'company1' logs in, he can only see user_code and company_code associated with company1.
How can i select only those attributes with 'company1'? Is there any function which provides all the attributes for a single column?
You will need to do this via the data provider for example
public function myDataProvider()
{
$id = Yii::app()->user->name;
$dataProvider=new CActiveDataProvider('MODELNAME', array(
'criteria'=>array(
'condition'=>'company_code= :id',
'params' => array(':id' => $id),
),
'pagination'=>array(
'pageSize'=>5,
),
));
return $dataProvider;
}
I presume that company1 is the users name. If not simply change it to say username/id etc.
Many thanks to Veelen for suggested edit (using params protects against sql injections).

CodeIgniter MVC and updating two different tables in one function

I have a JavaScript based page where a grid displays all the data for the registered users which is taken from the database.In the UI I have also column named 'groups' which display the groups that every users is participating, but this info is taken using additional two additional tables - 'groups' with 'id' and 'group_name' as columns, and a table 'users_groups' which has 'user_id' and 'group_id' as columns which are foreign keys associated with 'users' and 'groups' tables.
The problem is that when you click to edit any user from the UI grid there an AJAX that post JSON to containing the information for the user which include the
'groups' column which actually is not part of the 'users' table. So I have this:
$data = json_decode($this->input->post('data'), true);
Which originally (when the groups weren't added yet) was transformed in this:
$data = array(
'id' => $data['id'],
'email' => $data['email'],
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'usertype' => $data['usertype']
);
And then parsed to the model named 'mUsers' where is the update function:
$query = $this->mUsers->update($data);
And the model since now was very simple because there was only one table:
public function update($data)
{
$this->db->where('id', $data['id']);
$query = $this->db->update('users', $data);
return true;
}
But now I have to update another table - 'users_groups' and I'm thinking about my options.Is it possible to just add to the $data array show above another record:
'groups' => $data['groups']
And try to implement all the logic in one function(If this even possible, I couldn't find info if I can have two different active record queries in one function) or the second thing I thought of was after decoding the JSON to make something like:
$dataGroups = array(
'groups' => $data['groups'] //adding groups data to be fetched for update
);
and then parsing the data to a new function, something like:
$q = $this->mUsers->updateGroups($dataGroups);
What would be the better approach in this case?
Thanks
Leron
Make both function. And call them when necessary also you will be able to to call only usergroup function alone
//this function update all user data including group
public function updateUser($data, $changeGroup = true)
{
$this->db->where('id', $data['id']);
$query = $this->db->update('users', $data);
if($changeGroup) {
$this->updateUserGroup($data['id'], $data['groups']);
}
}
//Update user group
public function updateUserGroup($userid, $usergroups)
{
//Update user groups
}

help filtering data in cakephp

Im new to cakephp and Im having a little problem querying data.
I have a User model and a Product model in a many to many relation.
What I what is simply for my Products/Index action to only get the products associated to that User (the user is stored in session) and not all Products (which is what it does by default).
Please help.
You only need to set up the relationship properly, the rest is automatic.
Model:
class User extends AppModel {
var $hasAndBelongsToMany = array(
'Product' => array( /* set up relationship */ )
);
}
Controller:
$this->User->recursive = 2; // just to make sure, shouldn't be necessary
$user = $this->User->read(null, $userId);
debug($user);
/**
* $user['User'] contains the user data
* $user['Product'] contains associated products
*/
This should do the trick:
$products = $this->Product->find('all', array(
'conditions' => array(
'User.id' => $user_id_from_session
)
));

Categories