public function create(array $input)
{
if ($this->query()->where('name', $input['name'])->first()) {
throw new GeneralException(trans('exceptions.backend.access.roles.already_exists'));
}
//See if the role has all access
$all = $input['associated-permissions'] == 'all' ? true : false;
if (!isset($input['permissions'])) {
$input['permissions'] = [];
}
//This config is only required if all is false
if (!$all) {
//See if the role must contain a permission as per config
if (config('access.roles.role_must_contain_permission') && count($input['permissions']) == 0) {
throw new GeneralException(trans('exceptions.backend.access.roles.needs_permission'));
}
}
DB::transaction(function () use($input, $all) {
$role = self::MODEL;
$role = new $role();
$role->name = $input['name'];
$role->sort = isset($input['sort']) && strlen($input['sort']) > 0 && is_numeric($input['sort']) ? (int) $input['sort'] : 0;
//See if this role has all permissions and set the flag on the role
$role->all = $all;
if (parent::save($role)) {
if (!$all) {
$permissions = [];
if (is_array($input['permissions']) && count($input['permissions'])) {
foreach ($input['permissions'] as $perm) {
if (is_numeric($perm)) {
array_push($permissions, $perm);
}
}
}
$role->attachPermissions($permissions);
}
event(new RoleCreated($role));
return true;
}
throw new GeneralException(trans('exceptions.backend.access.roles.create_error'));
});
}