public function add() { $this->checkPageAction($this->page_id, Action::$Add); if (Request::ajax()) { if (Input::has('actionType')) { switch (Input::get('actionType')) { case 'checkGroupName': if (Input::has('group_name') && Input::has('company_id')) { $group_name = Input::get('group_name'); $company_id = Input::get('company_id'); $group = Group::where(array('group_name' => $group_name, 'company_id' => $company_id))->get(); if (count($group) > 0) { return 'false'; } } return 'true'; break; case 'addGroup': if (Request::isMethod('post') && Input::has('company_id') && Input::has('group_name') && Input::has('data')) { $data = json_decode(Input::get('data'), TRUE); $group_name = Input::get('group_name'); $company_id = Input::get('company_id'); $group = Group::where(array('group_name' => $group_name, 'company_id' => $company_id))->get(); if (count($group) > 0 || count($data) == 0) { return json_encode(array('msg' => 'Unable to add group, Please try again later!')); } else { $group = new Group(); $group->group_name = $group_name; $group->company_id = $company_id; if ($group->save()) { //$group_id = $group->group_id; $permissions = array(); foreach ($data as $permission_data) { $permissions[] = new Permission($permission_data); } if ($group->permission()->saveMany($permissions)) { return json_encode(array('status' => 'true', 'msg' => 'Successfully Added Group!')); } else { $group->delete(); return json_encode(array('status' => 'false', 'msg' => 'Unable To Add Group!')); } } return json_encode(array('status' => 'false', 'msg' => 'Unable To Add Group!')); } } return json_encode(array('status' => 'false', 'msg' => 'Invalid Request!')); break; } } } $user = User::find(Auth::id()); $group_id = $user->group_id; $company_id = $user->group->company_id; return view('content.setting.group.form.group_form', array('group_id' => $group_id, 'company_id' => $company_id)); }
public function generateGroups($company_id) { $group_list = array('0' => 'Admin'); $permission_list = array('0' => array('1' => array(array('page_id' => 1, 'action_value' => Action::$View | Action::$Add | Action::$Edit | Action::$Delete), array('page_id' => 2, 'action_value' => Action::$View | Action::$Add | Action::$Edit)), '2' => array(array('page_id' => 4, 'action_value' => Action::$View | Action::$Add | Action::$Edit | Action::$Delete)))); foreach ($group_list as $key => $group) { $new_group = new Group(); $new_group->fill(array('group_name' => $group, 'company_id' => $company_id)); if ($new_group->save()) { foreach ($permission_list[$key] as $module_key => $pages) { foreach ($pages as $page) { $new_permission = new Permission(); $new_permission->group_id = $new_group->group_id; $new_permission->module_id = $module_key; $new_permission->page_id = $page['page_id']; $new_permission->action_value = $page['action_value']; $new_permission->save(); } } } } }