Пример #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param \Illuminate\Http\Request $request
  *
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $this->validate($request, ['vote' => 'required|array|vote_count|vote_unique|sane_votes']);
     $user = $request->user();
     if ($user->uuid === null) {
         $user->update(['uuid' => uuid()]);
     }
     foreach ($request->get('vote') as $vote) {
         Vote::create(['candidate_id' => Candidate::findOrFail($vote)->id, 'user_id' => $user->id, 'term_id' => nextTerm()->id]);
     }
     Session::flash('message', 'Your votes were successfully counted.');
     return redirect('/');
 }
Пример #2
0
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     Validator::extend('vote_count', function ($attribute, $value, $parameters, $validator) {
         return count($value) <= env('NUM_ADMINS') && count($value) > 0;
     });
     Validator::extend('vote_unique', function ($attribute, $value, $parameters, $validator) {
         return Auth::user()->canVote();
     });
     Validator::extend('sane_votes', function ($attribute, $value, $parameters, $validator) {
         $can = [];
         foreach ($value as $candidate) {
             if (Candidate::findOrFail($candidate)->term_id !== nextTerm()->id || in_array($candidate, $can)) {
                 return false;
             }
             $can[] = $candidate;
         }
         return true;
     });
 }