コード例 #1
0
 public function store(Request $request)
 {
     if (Auth::user()->role != "supadmin" && $request->input('type') == "supadmin") {
         abort(401);
     }
     $rules = ['email' => 'required|email|unique:users', 'type' => 'required', 'names' => 'required', 'coordinates' => 'required', 'address' => 'required', 'mobile' => 'required', 'birth_date' => 'regex:/[0-9]{2}\\/[0-9]{2}\\/[0-9]{4}/', 'avatar' => 'image|mimes:jpg,jpeg,bmp,png,gif,tiff', 'cv' => 'mimes:doc,docx,ppt,pps,pptx,ppsx,xls,xlsx'];
     if (Auth::user()->role == "supadmin") {
         $rules['company'] = 'required';
     }
     $this->validate($request, $rules);
     $avatar_file_name = null;
     $cv_file_name = null;
     if ($request->file('avatar')) {
         $avatar_file_name = Common::randString(6) . "." . $request->file('avatar')->getClientOriginalExtension();
         Storage::put('avatars/' . $avatar_file_name, file_get_contents($request->file('avatar')->getRealPath()));
         ImageResize::load(storage_path("app") . '/avatars/' . $avatar_file_name);
         ImageResize::resizeToWidth(300);
         ImageResize::save();
     }
     if ($request->file('cv')) {
         $cv_file_name = Common::randString(6) . "." . $request->file('cv')->getClientOriginalExtension();
         Storage::put('cv/' . $cv_file_name, file_get_contents($request->file('cv')->getRealPath()));
     }
     $password = Common::generateStrongPassword();
     if (Auth::user()->role == "supadmin") {
         $company_id = $request->input('company');
     } else {
         $company_id = $this->company_id[0];
     }
     $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
     $user = new User();
     $user->role = $request->input('type');
     $user->password = Hash::make($password);
     $user->email = $request->input('email');
     $user->events_color = '#' . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)];
     $user->save();
     $info = new PersonalInfo();
     $info->names = $request->input('names');
     $info->address = $request->input('address');
     $info->coordinates = $request->input('coordinates');
     $info->mobile = $request->input('mobile');
     $info->gender = $request->input('gender');
     $info->birth_date = $request->input('birth_date');
     $info->home_phone = $request->input('home_phone');
     $info->work_phone = $request->input('work_phone');
     $info->fax = $request->input('fax');
     $info->other = $request->input('other');
     $info->avatar = $avatar_file_name;
     $info->cv = $cv_file_name;
     $user->info()->save($info);
     Company::find($company_id)->users()->attach($user->id);
     Notification::add($user->id, 'USER_ADD_BY_ADMIN', ['admin_id' => Auth::user()->id]);
     Mail::send('emails.newUserPassword', ['user' => $user, 'info' => $info, 'password' => $password, 'company' => Company::find($company_id)->pluck('name')], function ($m) use($user) {
         $m->from('*****@*****.**', 'TIMELINE');
         $m->to($user->email, $user->names)->subject('You have been added to TIMELINE');
     });
     return redirect('/users')->with(['message' => "User added successfully. Generated password with further login information is sent to his/her email ({$request->input('email')})."]);
 }