mercredi 28 octobre 2015

Check if Password exists

I'm using laravel 5.1. Now I want to retrieve check if a certain password is already defined in the database. Here's my database schema

/* Accounts table */

Schema::create('accounts', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('pharmacist_id')->unsigned()->index();
      $table->foreign('pharmacist_id')->references('id')->on('pharmacists')->onDelete('cascade');
      $table->string('username')->unique();
      $table->string('password', 70);
      $table->string('rights');
      $table->rememberToken()
      $table->timestamps();
});

/* Pharmacists Table */

Schema::create('pharmacists', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('pharmacy_id')->unsigned()->index();
      $table->foreign('pharmacy_id')->references('id')->on('pharmacies')->onDelete('cascade');
      $table->string('fname');
      $table->string('mname');
      $table->string('lname')
      $table->date('bdate');
      $table->string('email');
      $table->string('contact');
      $table->timestamps();
});

Now what I want is to check if a certain password is already defined in a certain pharmacy_id it looks something like this

$accounts = Account::whereHas('pharmacist', function($query) {
                    return $query->where('pharmacy_id', Auth::user()->id);
                })->where('password', $password)->get();

But it seems that the password is only being passed as a plain text and not encrypted. Also I tried using this method

where('password', bcrypt($password))
where('password', Hash::make($password))
where('password', Crypt::encrypt($password))

But none of this works. Any solution guys? I'm thinking of something like this and I'm not sure if this is possible

$is_valid = Auth::validate(array('pharmacist.pharmacy_id' => Auth::user()->id, 'password' => $value));

Because if I used the below code I can able to check if the user has inputted the valid password.

$is_valid = Auth::validate(array('id' => Auth::user()->id, 'password' => $value));

It's easy to check if the username and password match using the Auth::validate but the needed checking is to check if a certain pharmacist already inputted this specific password. So basically its kinda like looping in all the accounts and check if their password is the same as this specific password.

Here's what I have so far but this has some problem. If a certain pharmacy has 1000+ user then this will loop 1000x which is not optimized and not a good solution

$accounts = Account::whereHas('pharmacist', function($query) {
                    return $query->where('pharmacy_id', Auth::user()->id);
                })->get();


foreach($accounts as $account) {
    if (Hash::check($value, $account->password)) {
      // FOUND!!!
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1RdFGnM
via IFTTT

Aucun commentaire:

Enregistrer un commentaire