/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(GarageRequest $request) { $data = $request->all(); $admin_array = array('username' => $data['username'], 'password' => bcrypt($data['password']), 'email' => $data['email'], 'realname' => $data['realname'], 'phone' => $data['phone'], 'user_type' => 'manager'); try { DB::transaction(function () use($admin_array, $data) { // 新建车厂管理员 $admin_id = DB::table('admins')->insertGetId($admin_array); $admin = User::where('id', $admin_id)->first(); // $admin = DB::table('admins')->where('id', $admin_id)->first(); $role = Role::where('name', 'Garage')->first(); // $role = DB::table('roles')->where('name', 'Garage')->first(); $admin->attachRole($role); // 设置车厂管理员权限 $garage_array = array('admin_id' => $admin_id, 'name' => $data['name'], 'branch_name' => $data['branch_name'], 'type' => $data['type'], 'location_detail' => $data['location_detail']); DB::table('garages')->insertGetId($garage_array); return redirect()->route('admin.checkgarage.index')->with('message', '成功添加新车厂!'); }); } catch (Exception $e) { return redirect()->back()->withInput($request->input())->with('fail', '添加车厂失败,数据库操作返回异常!'); } // if ($admin->id) { //添加管理员成功 // $garage->save(); // if($garage->id) { // 添加车厂(及管理员)成功 // return redirect()->route('admin.checkgarage.index')->with('message', '成功添加新车厂!'); // } // else { //添加失败 // return redirect()->back()->withInput($request->input())->with('fail', '添加管理员成功但添加车厂失败,数据库操作返回异常!'); // } // } // else { //添加管理员失败 // return redirect()->back()->withInput($request->input())->with('fail', '添加管理员失败,数据库操作返回异常!'); // } }
/** * Run the database seeds. * * @return void */ public function run() { DB::table('admins')->delete(); $admin_founder = new User(); $admin_founder->username = $admin_founder->nickname = e('wuzifeng'); $admin_founder->password = bcrypt(e('admin')); $admin_founder->email = e('*****@*****.**'); $admin_founder->realname = e('wzf'); $admin_founder->user_type = 'manager'; //管理型用户 $admin_founder->confirmation_code = md5(uniqid(mt_rand(), true)); $admin_founder->confirmed = true; //确定用户已被验证激活 $admin_founder->save(); $admin_admin = new User(); $admin_admin->username = $admin_admin->nickname = e('admin'); $admin_admin->password = bcrypt(e('admin')); $admin_admin->email = e('*****@*****.**'); $admin_admin->realname = e('admin'); $admin_admin->user_type = 'manager'; //管理型用户 $admin_admin->confirmation_code = md5(uniqid(mt_rand(), true)); $admin_admin->confirmed = true; //确定用户已被验证激活 $admin_admin->save(); $admin_provider = new User(); $admin_provider->username = $admin_provider->nickname = e('provider'); $admin_provider->password = bcrypt(e('admin')); $admin_provider->email = e('*****@*****.**'); $admin_provider->realname = e('provider'); $admin_provider->user_type = 'manager'; //管理型用户 $admin_provider->confirmation_code = md5(uniqid(mt_rand(), true)); $admin_provider->confirmed = true; //确定用户已被验证激活 $admin_provider->save(); $admin_garage = new User(); $admin_garage->username = $admin_garage->nickname = e('garage'); $admin_garage->password = bcrypt(e('admin')); $admin_garage->email = e('*****@*****.**'); $admin_garage->realname = e('garage'); $admin_garage->user_type = 'manager'; //管理型用户 $admin_garage->confirmation_code = md5(uniqid(mt_rand(), true)); $admin_garage->confirmed = true; //确定用户已被验证激活 $admin_garage->save(); $admin_guest = new User(); $admin_guest->username = $admin_guest->nickname = e('guest'); $admin_guest->password = bcrypt(e('admin')); $admin_guest->email = e('*****@*****.**'); $admin_guest->realname = e('guest'); $admin_guest->user_type = 'manager'; //管理型用户 $admin_guest->confirmation_code = md5(uniqid(mt_rand(), true)); $admin_guest->confirmed = true; //确定用户已被验证激活 $admin_guest->save(); DB::table('roles')->delete(); $role_founder = new Role(); $role_founder->name = 'Founder'; $role_founder->display_name = '创始人'; // optional $role_founder->description = 'BiBi项目的创始人'; // optional $role_founder->save(); $role_partner = new Role(); $role_partner->name = 'Partner'; $role_partner->display_name = '合伙人'; // optional $role_partner->description = '与创始人共同创业者'; // optional $role_partner->save(); $role_admin = new Role(); $role_admin->name = 'Admin'; $role_admin->display_name = '超级管理员'; // optional $role_admin->description = 'BiBi后台系统的超级管理员'; // optional $role_admin->save(); $role_invester = new Role(); $role_invester->name = 'Invester'; $role_invester->display_name = '投资人'; // optional $role_invester->description = '项目的投资者'; // optional $role_invester->save(); $role_provider = new Role(); $role_provider->name = 'Provider'; $role_provider->display_name = '供应商'; $role_provider->description = '与BiBi合作,提供优惠券等服务的商家(暂时不包含车厂)'; $role_provider->save(); $role_garage = new Role(); $role_garage->name = 'Garage'; $role_garage->display_name = '车厂'; $role_garage->description = '与BiBi合作,提供优惠券等服务的车厂'; $role_garage->save(); $role_operator = new Role(); $role_operator->name = 'Operator'; $role_operator->display_name = '操作员'; $role_operator->description = '审查供应商以及优惠券等资料'; $role_operator->save(); $role_editor = new Role(); $role_editor->name = 'Editor'; $role_editor->display_name = '编辑'; $role_editor->description = '官方网站内容编辑者'; $role_editor->save(); $role_guest = new Role(); $role_guest->name = 'Guest'; $role_guest->display_name = '客人'; $role_guest->description = '仅供演示'; $role_guest->save(); DB::table('permissions')->delete(); $permission_manage_contents = new Permission(); $permission_manage_contents->name = 'manage_contents'; $permission_manage_contents->display_name = '管理内容'; // optional // Allow a admin to... $permission_manage_contents->description = '管理后台所有内容'; // optional $permission_manage_contents->save(); $permission_manage_users = new Permission(); $permission_manage_users->name = 'manage_users'; $permission_manage_users->display_name = '管理用户'; $permission_manage_users->description = '管理后台所有用户'; $permission_manage_users->save(); $permission_manage_system = new Permission(); $permission_manage_system->name = 'manage_system'; $permission_manage_system->display_name = '管理系统'; $permission_manage_system->description = '管理后台系统'; $permission_manage_system->save(); $permission_manage_providers = new Permission(); $permission_manage_providers->name = 'manage_providers'; $permission_manage_providers->display_name = '管理供应商'; $permission_manage_providers->description = '管理后台所有供应商'; $permission_manage_providers->save(); $permission_manage_slef_garage = new Permission(); $permission_manage_slef_garage->name = 'manage_slef_garage'; $permission_manage_slef_garage->display_name = '管理车厂自身'; $permission_manage_slef_garage->description = '管理车厂自身资料'; $permission_manage_slef_garage->save(); $permission_manage_self_coupons = new Permission(); $permission_manage_self_coupons->name = 'manage_self_coupons'; $permission_manage_self_coupons->display_name = '管理自身优惠券'; $permission_manage_self_coupons->description = '管理供应商自身优惠券'; $permission_manage_self_coupons->save(); $permission_check_providers = new Permission(); $permission_check_providers->name = 'check_providers'; $permission_check_providers->display_name = '审查所有供应商'; $permission_check_providers->description = '审查所有供应商资料'; $permission_check_providers->save(); $permission_check_garages = new Permission(); $permission_check_garages->name = 'check_garages'; $permission_check_garages->display_name = '审查所有车厂'; $permission_check_garages->description = '审查所有车厂资料'; $permission_check_garages->save(); $permission_check_coupons = new Permission(); $permission_check_coupons->name = 'check_coupons'; $permission_check_coupons->display_name = '审查所有优惠券'; $permission_check_coupons->description = '审查所有供应商的优惠券资料'; $permission_check_coupons->save(); $permission_view_coupons_used = new Permission(); $permission_view_coupons_used->name = 'view_coupons_used'; $permission_view_coupons_used->display_name = '查看已使用优惠券'; $permission_view_coupons_used->description = '查看所有用户优惠券的使用情况'; $permission_view_coupons_used->save(); $permission_view_self_coupons_used = new Permission(); $permission_view_self_coupons_used->name = 'view_self_coupons_used'; $permission_view_self_coupons_used->display_name = '查看供应商已使用优惠券'; $permission_view_self_coupons_used->description = '查看供应商用户优惠券的使用情况'; $permission_view_self_coupons_used->save(); DB::table('permission_role')->delete(); $role_admin->attachPermissions(array($permission_manage_system, $permission_manage_contents, $permission_manage_users, $permission_check_garages, $permission_check_coupons)); $role_garage->attachPermissions(array($permission_manage_slef_garage, $permission_manage_self_coupons, $permission_view_self_coupons_used)); DB::table('role_admin')->delete(); // role attach alias $admin_admin->attachRole($role_admin); // parameter can be an Role object, array, or id $admin_garage->attachRole($role_garage); // or eloquent's original technique // $user->roles()->attach($admin->id); // id only }