【问题标题】:Adding permission dynamically with Sentry in Laravel在 Laravel 中使用 Sentry 动态添加权限
【发布时间】:2014-05-29 11:48:00
【问题描述】:

我已经填充了我的权限表以保存“pages.add”、“pages.edit”等值。在创建组时,我在复选框中填充了这些权限。在提交时,我遍历这些复选框的发布值并将它们放入一个数组中。然后我以这种方式创建组:

公共函数存储(){ $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);
}

我看到值已填充到数组中。哨兵仍然不会在组表中加载这些权限。怎么了?请提出建议。

【问题讨论】:

    标签: php arrays permissions laravel-4 cartalyst-sentry


    【解决方案1】:

    我如何在 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();
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-24
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多