Created
March 22, 2013 22:32
-
-
Save alihammad-gist/5225275 to your computer and use it in GitHub Desktop.
filters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
id | |
uid | |
module (id of the module) | |
entityId (0 = all) | |
c 0/1 | |
r 0/1 | |
u 0/1 | |
d 0/1 | |
**/ | |
Route::filter('upadte-player', function ($route, $req) { | |
$id = $route->getParameter('id'); | |
$mod = Auth::user(); | |
return $mod->canUpdatePlayer($id); | |
}); | |
Route::filter('update-team', function ($route, $req) { | |
$id = $route->getParameter('id'); | |
$mod = Auth::user(); | |
return $mod->canUpdateTeam($id); | |
}); | |
Route::filter('create-player', function ($route, $req) { | |
$mod = Auth::user(); | |
return $mod->canCreatePlayers(); | |
}); | |
Route::filter('create-team', function ($route, $req) { | |
$mod = Auth::user(); | |
return $mod->canCreateTeams(); | |
}); | |
Route::filter('delete-player', function ($route, $req) { | |
$mod = Auth::user(); | |
return $mod->canDeletePlayers(); | |
}); | |
Router::filter('delete-team', function ($route, $req) { | |
$mod = Auth::user(); | |
return $mod->canDeleteTeams(); | |
}); | |
// user model methods | |
public function canUpdatePlayers($id) { | |
if($this->isAdmin()) return true; | |
$mod = $this->mods()->where('module', MODULE_PLAYER_ID) | |
->andWhere('entityId', $id)->orWhere('entityId', 0); | |
if(!$mod) return false; | |
return ((int) $mod->u); | |
} | |
public function canUpdateTeam($id) { | |
if($this->isAdmin()) return true; | |
$mod = $this->mods()->where('module', MODULE_TEAM_ID) | |
->andWhere('entityId', $id)->orWhere('entityId', 0); | |
if(!$mod) return false; | |
return ((int) $mod->u); | |
} | |
public function canCreateTeams() { | |
if($this->isAdmin()) return true; | |
$mod = $this->mods()->where('module', MODULE_TEAM_ID) | |
->andWhere('entityId', 0); | |
if(!$mod) return false; | |
return ((int) $mod->c); | |
} | |
public function canCreatePlayers() { | |
if($this->isAdmin()) return true; | |
$mod = $this->mods()->where('module', MODULE_TEAM_ID) | |
->andWhere('entityId', 0); | |
if(!$mod) return false; | |
return ((int) $mod->c); | |
} | |
public function canDeletePlayers() { | |
if($this->isAdmin()) return true; | |
$mod = $this->mods()->where('module', MODULE_PLAYER_ID) | |
->andWhere('entityId', 0); | |
if(!$mod) return false; | |
return ((int) $mod->d); | |
} | |
public function canDeleteTeams() { | |
if($this->isAdmin()) return true; | |
$mod = $this->mods()->where('module', MODULE_TEAM_ID) | |
->andWhere('entityId', 0); | |
if(!$mod) return false; | |
return ((int) $mod->d); | |
} | |
public function isAdmin() { | |
if(Session::get('uType', 'guest') == 'admin') return true; | |
return false; | |
} | |
public function isPlayerMod() { | |
if($this->canDeletePlayer()) return true; | |
return false; | |
} | |
public function isTeamMod() { | |
if($this->canDeleteTeams()) return true; | |
return false; | |
} | |
public function promoteToMod($user, $module) { | |
if(!$this->isAdmin) return false; | |
$user->mods()->where('module', $module)->delete(); | |
return MODS::create([ | |
'uid' => $uid, | |
'module' => MODULE_TEAM_ID, | |
'entityId' => 0, | |
'c' => 1, | |
'r' => 1, | |
'u' => 1, | |
'd' => 1 | |
]); | |
} | |
public function promoteToPlayerMod($user) { | |
return $this->promote($user, MODULE_PLAYER_ID); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment