Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:16:08+00:00 2026-06-13T23:16:08+00:00

Using CakePHP 2.2, I am building an application in which each client has it’s

  • 0

Using CakePHP 2.2, I am building an application in which each client has it’s own “realm” of data and none of the other data is visible to them. For example, a client has his set of users, courses, contractors and jobs. Groups are shared among clients, but they cannot perform actions on groups. All clients can do with groups is assign them to users. So, an administrator (using ACL) can only manage data from the same client id.

All my objects (except groups, of course) have the client_id key.

Now, I know one way to get this done and actually having it working well, but it seems a bit dirty and I’m wondering if there is a better way. Being early in the project and new to CakePHP, I’m eager to get it right.

This is how I’m doing it now :

1- A user logs in. His client_id is written to session according to the data from the user’s table.

$user = $this->User->read(null, $this->Auth->user('id'));
$this->Session->write('User.client_id', $user['User']['client_id']);

2- In AppController, I have a protected function that compares that session id to a given parameter.

protected function clientCheck($client_id) {
    if ($this->Session->read('User.client_id') == $client_id) {
        return true;
    } else {
        $this->Session->setFlash(__('Invalid object or view.'));
        $this->redirect(array('controller' => 'user', 'action' => 'home'));
    }
}

3- Im my different index actions (each index, each relevant controller), I check the client_id using a paginate condition.

public function index() {
    $this->User->recursive = 0;
    $this->paginate = array(
         'conditions' => array('User.client_id' => $this->Session->read('User.client_id'))
    );
    $this->set('users', $this->paginate());
}

4- In other actions, I check the client_id before checking the HTTP request type this way.

$user = $this->User->read(null, $id);
$this->clientCheck($user['User']['client_id']);
$this->set('user', $user);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:16:09+00:00Added an answer on June 13, 2026 at 11:16 pm

    The concept is good – it’s not ‘dirty’, and it’s pretty much exactly the same as how I’ve handled situations like that.

    You’ve just got a couple of lines of redundant code. First:

    $this->Auth->user('id')
    

    That method can actually get any field for the logged in user, so you can do:

    $this->Auth->user('client_id')
    

    So your two lines:

    $user = $this->User->read(null, $this->Auth->user('id'));
    $this->Session->write('User.client_id', $user['User']['client_id']);
    

    Aren’t needed. You don’t need to re-read the User, or write anything to the session – just grab the client_id directly from Auth any time you need it.

    In fact, if you read http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user it even says you can get it from outside the context of a controller, using the static method like:

    AuthComponent::user('client_id')
    

    Though it doesn’t seem you’ll be needing that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a web application using CakePHP. I am using meioupload to upload
I'm building a prototype search application using CakePHP and I have a search controller
I'm building a web application in CakePHP. I'm using the Paginator object to render
I am building an application using cakePHP. Do we have a method where we
Using CakePHP 1.3 , I post a form which correctly fills in $this->data .
Using CakePHP 1.3 I understand the example at http://book.cakephp.org/complete/1143/Data-Validation#Adding-your-own-Validation-Methods-1181 on how to add my
Im using cakephp 2.0 and i have data submitted that i want to cleanup,
I am using CakePHP. I created an external class which is not a model
I am building a PHP application with CakePHP 2 . I need to have
I am building a web app using CakePHP that will allow users to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.