开发者

Data Filtering in Cakephp

Please forgive me, I am still fairly new to cakephp and in the process of reviving my redundant PhP skills.

I have searched all over the "interweb" for the answer, sadly to no avail.

Here is the scenario:

I have 2 tables - 1 being Users the other Groups ( for permissions).

Users table - pretty standard plus some additional profile info.

Groups table - holds Administrators, Super Users, Auth Users and Unauth Users.

The Users table is being used for authentication and profiles (other users can browse user profiles). Now with this in mind, I cannot work out how to filter out which users get rendered into the index view based on which group the users (not the the currently logged user) belong.

For example, The admin and Super user accounts profiles are being rendered and belongs to the "Administrators" and "Super users" groups respectively along with all the other Users. However all I want end users (Auth Users) to be able to see are other user profiles which are part of the Auth Users group.

So any and all help w开发者_StackOverflow中文版ould be appreciated for both saving the remainder of my hair and finding a resolve to this problem I have.

Ok so here is what I did which is working like a charm. Configured the paginate variable to look like this:

var $paginate = array(
            'fields' => array(
                    'User.img_file', 'User.given_name', 'User.family_name', 'Group.name', 'User.username', 'User.created'),
            'limit' => 10,
            'order' => array(
                'User.given_name' => 'asc'
            )
        );

Then for the in the index function (which I wanted) I added the following:

function index() {
                $this->User->recursive = 0;
                $this->paginate = array(   
                'conditions' => array(
                                  'group_id' => 3
                ));
                $users = $this->paginate('User');
                $this->set(compact('users'));
}

It's working like I want it to, but if anything does look malformed, or this can be extended, please do post comments.


First off, take a look at this for information on using the built in authentication features of cakephp.

" I cannot work out how to filter out which users get rendered into the index view based on which group the users (not the the currently logged user) belong."

not 100% on what you mean by this, but if you mean you only want to see other users that are admins (and assuming you know that the admin_group_id == 1) then, what you kind of want is the following.

$administrator_group_id = 1;

$conditions = array(
  'group_id' => "{$administrator_group_id}"
);

$this->data = $this->User->findAll($conditions); 
// note. automatically makes the result data available in the VIEW layer

pr($this->data); // VARDUMP utility

(ps. check this out for how to paginate this result data)

"...all I want end users (Auth Users) to be able to see are other user profiles which are part of the Auth Users group"

(assuming the auth stuff didn't help you.)

(if there are only 3 types of group types, and I can control them completely, then I would consider not using a group table, and using an enumeration. here are notes on how to do this in MySQL

http://dev.mysql.com/doc/refman/5.0/en/enum.html

or, you can just sort of hack it in PHP.

eg.

define('USER', 1);
define('S_USER', 10);
define('ADMIN', 100);

$user = $this->User->findById($my_user_id); // or get from auth

$conditions = array( "group_id <= " => "{$user['User']['group_id']}" );

$this->data = $this->User->findAll($conditions); pr($this->data); // VARDUMP

( what I did was that you get the logged in user, and I made sure that the ADMIN had the highest level ID. SuperUsers had the 2nd, and Users the lowest. This query will return all users that are on the same level, or lower than their own level. group_id <= User.group_id)

I hope I haven't confused you too much. Just keep on experimenting with cake. It's worth it!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜