SHARE
TWEET

Untitled

a guest May 27th, 2018 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #modified code wherein admin can only register new users
  2.  
  3. <?php
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\User;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Str;
  10. use Mail;
  11. use App\Mail\verifyEmail;
  12. //imageuplod
  13. use Illuminate\Support\Facades\File;
  14. use Illuminate\Support\Facades\URL;
  15. use Illuminate\Support\Facades\Input;
  16. use Illuminate\Support\Facades\DB;
  17. //email
  18.  
  19.  
  20. class UserController extends Controller
  21. {
  22.     /**
  23.      * Display a listing of the resource.
  24.      *
  25.      * @return \Illuminate\Http\Response
  26.      */
  27.     public function index()
  28.     {
  29.         $users = User::all();
  30.  
  31.         //Load all users on the table and pass the users
  32.         $users = User::where(['archive'=>1])
  33.         ->whereIN('roles', [1,2,3])
  34.         ->orderBy('id')
  35.         ->get();
  36.         return view('usercrud.index')->with('users', $users);      
  37.     }
  38.  
  39.     /**
  40.      * Show the form for creating a new resource.
  41.      *
  42.      * @return \Illuminate\Http\Response
  43.      */
  44.     public function create(Request $request)
  45.     {
  46.         //
  47.         $usersC = new User;
  48.         $url = '';
  49.         $usersC->position = $request->companyPos;
  50.         $usersC->empid = $request->employeeid;
  51.         $usersC->name = $request->fullname;
  52.         $usersC->email = $request->email;
  53.         $usersC->password = bcrypt($request->password);
  54.         $usersC->roles = $request->role_id;
  55.         //token email
  56.        
  57.         $token= str_random(25);
  58.         $usersC->verifyToken=$token;
  59.  
  60.         //image uplod
  61.         if(Input::hasFile('images')){
  62.            
  63.             $file = Input::file('images');
  64.             $file->move(public_path(). '/uploads/', $file->getClientOriginalName());
  65.             $url = '/uploads/'. $file->getClientOriginalName();
  66.            
  67.  
  68.         }
  69.         $usersC-> image = $url;
  70.         $usersC -> save();
  71.  
  72.         Mail::send('mail.confirmation', $token, function($message) use($token){
  73.             $message->'email';
  74.             $message->subject('Registration Confirmation');
  75.         });
  76.  
  77.         $users = User::all();
  78.          $users = User::where(['archive'=>1])->orderBy('id')->first();
  79.        
  80.         //email verification
  81.        
  82.        
  83.         // $thisUser = User::findOrFail($users->id);
  84.         //  $this->sendEmail($thisUser);
  85.         //$users = User::where(['archive'=>1])->orderBy('id')->get();
  86.  
  87.         return redirect()->to('userIndex')->with('users','message', $users, 'confirmation is email was sent');
  88.    
  89.        
  90.     }
  91.  
  92.     /**
  93.      * Store a newly created resource in storage.
  94.      *
  95.      * @param  \Illuminate\Http\Request  $request
  96.      * @return \Illuminate\Http\Response
  97.      */
  98.     public function store(Request $request)
  99.     {
  100.         //
  101.     }
  102.  
  103.     /**
  104.      * Display the specified resource.
  105.      *
  106.      * @param  int  $id
  107.      * @return \Illuminate\Http\Response
  108.      */
  109.     public function show($id)
  110.     {
  111.         //
  112.     }
  113.  
  114.     /**
  115.      * Show the form for editing the specified resource.
  116.      *
  117.      * @param  int  $id
  118.      * @return \Illuminate\Http\Response
  119.      */
  120.     public function edit($id)
  121.     {
  122.         //
  123.         $users = User::where(['id'=>$id])->first();
  124.  
  125.  
  126.         // dd($users);
  127.  
  128.          return view('usercrud.edit',compact('users'));
  129.     }
  130.  
  131.     /**
  132.      * Update the specified resource in storage.
  133.      *
  134.      * @param  \Illuminate\Http\Request  $request
  135.      * @param  int  $id
  136.      * @return \Illuminate\Http\Response
  137.      */
  138.     public function update(Request $request, $id)
  139.     {
  140.         //
  141.         $usersUp = new User;
  142.         $url = '';
  143.         $password = bcrypt($request->editpassword);
  144.         if(Input::hasFile('images')){
  145.            
  146.             $file = Input::file('images');
  147.             $file->move(public_path(). '/uploads/', $file->getClientOriginalName());
  148.             $url = '/uploads/'. $file->getClientOriginalName();
  149.            
  150.  
  151.         }
  152.  
  153.  
  154.         // dd($request->editcompanyPos);
  155.  
  156.         $usersUp = User::where('id',$id)
  157.             ->update(['position'=>$request->editcompanyPos,'empid'=>$request->editemployeeid, 'name'=>$request->editfullname,'email'=>$request->editemail,'password'=>$password,'roles'=>$request->editrole_id, 'image'=>$url]);
  158.  
  159.         $users = User::all();
  160.         $users = User::where(['archive'=>1])->orderBy('id')->get();
  161.  
  162.         return redirect()->to('userIndex')->with('users', $users);
  163.     }
  164.     /**
  165.      * Remove the specified resource from storage.
  166.      *
  167.      * @param  int  $id
  168.      * @return \Illuminate\Http\Response
  169.      */
  170.     public function destroy($id)
  171.     {
  172.         $userDel = User::where('id',$id)->update(['archive'=>0]);
  173.  
  174.         $users = User::all();
  175.         $users = User::where(['archive'=>1])->orderBy('id')->get();
  176.  
  177.         return redirect()->to('userIndex')->with('users', $users);
  178.  
  179.     }
  180.     public function verifyEmailFirst(Request $request)
  181.     {
  182.         //
  183.         return view('usercrud.verifyEmailFirst');
  184.     }
  185.     public function sendEmail($thisUser)
  186.     {
  187.         Mail::to($thisUser['email'])->send(new verifyEmail($thisUser));
  188.     }
  189.  
  190.     public function sendEmailDone($email, $verifyToken)
  191.     {
  192.         $user = User::where(['email'=>$email, 'verifyToken'=>$verifyToken])->first();
  193.         //return $user;
  194.         if ($user){
  195.             return user::where(['email'=>$email, 'verifyToken'=>$verifyToken])->update(['status'=>'1','verifyToken'=>NULL]);
  196.  
  197.         }else{
  198.             return 'user not found';
  199.         }
  200.  
  201.    
  202.     }
  203.     public function enableview()
  204.     {
  205.         $users = User::all();
  206.  
  207.         //Load all users on the table and pass the users
  208.         $users = User::where(['archive'=>0])->orderBy('id')->get();
  209.         return view('usercrud.enable')->with('users', $users);
  210.  
  211.     }
  212.  
  213.     public function enable($id)
  214.     {
  215.         $userEnable = User::where('id',$id)->update(['archive'=>1]);
  216.  
  217.         $users = User::all();
  218.         $users = User::where(['archive'=>0])->orderBy('id')->get();
  219.  
  220.         return redirect()->to('userEnableView')->with('users', $users);
  221.  
  222.     }
  223.  
  224.     //email
  225.     protected function register(Request $request){
  226.             $input = $request->all();
  227.             $validator = $this->validator($input);
  228.  
  229.             if ($validator->passes()){
  230.                 $data = $this->create($input)->toArray();
  231.                 $data['token'] = str_random(25);
  232.  
  233.                 $user= User::find($data['id']);
  234.                 $user->token=$data['token'];
  235.                 $user->save();
  236.  
  237.                 Mail::send('mail.confirmation', $data, function($message) use($data){
  238.                     $message->to($data['email']);
  239.                     $message->subject('Registration Confirmation');
  240.  
  241.                 });
  242.                 return redirect(route('login'))->with('status','confirmation email has been sent');
  243.             }
  244.             return redirect(route('login'))->with('status', $validator->errors());
  245.         }
  246.  
  247.     public function confirmation($token){
  248.         $user = User::where('token', $token)->first();
  249.  
  250.         if(!is_null($user)){
  251.             $user->confirmed = 1;
  252.             $user->token="";
  253.             $user->save();
  254.             return redirect(route('login'))->with('status','your activation is completed');
  255.         }
  256.         return redirect(route(login))->with('status','something went wrong');
  257.     }
  258.  
  259.  
  260.  
  261. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top