[英]Adding permission dynamically with Sentry in Laravel
我已經填充了“權限”表以容納“ pages.add”,“ pages.edit”等值。在創建組時,我在復選框中填充了這些權限。 在提交時,我遍歷這些復選框的發布值,並將它們放入數組中。 然后,我以這種方式創建組:
公共功能store(){$ validation = new CreateGroupValidator;
if ($validation->passes()) { try { // Create the group $permissions = Input::get('permissions'); $pers=array(); foreach($permissions as $p) { $pname=Permission::find($p)->name; array_add($pers,$pname,'1'); } $group = Sentry::createGroup(array( 'name' => Input::get('name'), 'permissions' => $pers )); return Redirect::route('admin.groups.index'); } catch (\\Cartalyst\\Sentry\\Groups\\GroupExistsException $e) { Notification::error('A group with same name already exists.'); } } return Redirect::back()->withInput()->withErrors($validation->errors); }
我看到值已填充到數組中。 仍然崗哨將不會在組表中加載這些權限。 怎么了? 請提出建議。
我如何在HTML中使用復選框:
<input type="checkbox" name="perms[page.edit]" value="1"> Allow
<input type="checkbox" name="perms[page.add]" value="1"> Allow
然后這就是我實現我的更新功能的方式,但是我的創建和更新都使用類似的代碼
$input = Input::get('perms');
$c = [];
foreach ($input as $k => $p) {
$c[$k] = $p;
}
// Find the group using the group id
$group = Sentry::findGroupById(1);
// Update the group details
$group->name = Input::get('name');
$group->permissions = $c;
// Update the group
$group->save();
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.