Skip to content

Instantly share code, notes, and snippets.

@derekperkins
Created May 29, 2014 20:22
Show Gist options
  • Save derekperkins/247c7feb8634c58dd33e to your computer and use it in GitHub Desktop.
Save derekperkins/247c7feb8634c58dd33e to your computer and use it in GitHub Desktop.
// Profile.php
public $findMethods = [
'permissions' => true
];
public function find($type, $query = []) {
echo "Found it in the model!";die;
if ($type === 'permissions') {
echo "ASDFASDFSF";die;
}
return parent::find($type, $query);
}
protected function _findPermissionsaa($state, $query, $results = []) {
if ($state === 'before') {
$query['fields'] = [
'Workspace.id',
'Workspace.name',
'Workspace.status_id',
'UserWorkspaces.permissions',
'Profile.id',
'Profile.name',
'Profile.status_id',
'UserProfiles.permissions'
];
$query['joins'] = [
[ 'table' => 'users_profiles',
'alias' => 'UserProfiles',
'type' => 'INNER',
'conditions' => [
'Profile.id = UserProfiles.profile_id',
]
],
[ 'table' => 'workspaces',
'alias' => 'Workspace',
'type' => 'INNER',
'conditions' => [
'Workspace.id = Profile.workspace_id',
]
],
[ 'table' => 'users_workspaces',
'alias' => 'UserWorkspaces',
'type' => 'INNER',
'conditions' => [
'Workspace.id = UserWorkspaces.workspace_id',
]
]
];
$query['group'] = ['Workspace.id', 'Profile.id'];
return $query;
}
foreach ($results as $key => $result) {
$results[$key]['Workspace']['permissions'] = $result['UserWorkspaces']['permissions'];
$results[$key]['Profile']['permissions'] = $result['UserProfiles']['permissions'];
unset($results[$key]['UserWorkspaces']);
unset($results[$key]['UserProfiles']);
}
return $results;
}
// In an auth component
$this->Controller->Profile->find('permissions', ['conditions' => [
'UserProfiles.user_id' => $userId,
'UserWorkspaces.workspace_id' => $workspaceId,
'UserProfiles.profile_id' => $profileId
]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment