How to solve authentication in laravel 4? - php

When login correct username & password then show error
ErrorException
Argument 1 passed to
Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an
instance of Illuminate\Auth\UserInterface, instance of User given,
called in
E:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Auth\Guard.php
on line 316 and defined
Anybody know where problem?

Your User model MUST implement UserInterface and its methods:
<?php namespace Illuminate\Auth;
interface UserInterface {
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier();
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword();
}
So it must declared as something like
use Illuminate\Auth\UserInterface;
class User extends Eloquent implements UserInterface {
}

This error might also appear when you use a different model for users (example :member)
if it's the case you need to update Authentication Model and Authentication Table in app/config/auth.php with the appropriate model / database table.

Related

How to override sendPasswordResetNotification function outside of user model?

I am doing a password reset and sending email with Notification.
I created ResetPasswordNotification. I added the sendPasswordResetNotification method inside the User.php model.It works successfully. But since my User model is working in another common project, where can I write the sendPasswordResetNotification method outside of the User.php model.
my User.php model
/**
* Send the password reset notification.
*
* #param string $token
* #return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
In short, how can I use a method other than the user.php model to exclude the sendPasswordResetNotification method in the canResetPassword trait?
In the project where I use Notification, I opened the user model and extended the common user model and added the sendPasswordResetNotification method there. I produced a solution in this way.

How to getting current Email that be used for login in Myth/Auth library Code Igniter 4

I wanted to make a program that able to store a data and every the data that i send to the Database must contain the Email from user used to login to my Web. In here i'm using Myth Auth library as authenticator and login security. But i can't find the method to call the current Email so i can fetch it and push to my Database. i'm using MySql as my DB.
You have to create an Entity extending Mith's one.
So create app/Entities/User.php
Then... to use:
echo user()->getEmail();
See Documentation:
https://github.com/lonnieezell/myth-auth/blob/develop/docs/extending.md
<?php namespace App\Entities;
use Myth\Auth\Entities\User as MythUser;
class User extends MythUser
{
/**
* Returns Email
*
* #return string
*/
public function getEmail()
{
return trim(trim($this->attributes['email']));
}
}
add the following method to myth-auth\src\Entities\User.php
public function getEmail()
{
return trim(trim($this->attributes['email']));
}
then, anywhere in your app, you can call it with user()->getUsername()
You can do the same for username or any of the other user attributes.
A!
Now that we have our new entity, we need to update our UserModel to return it instead of the default Myth:Auth version:
class UserModel extends MythModel
{
protected $returnType = 'App\Entities\User';
...

Doctrine2: persisting parent entity given child class

I've got a User Entity defined (mapping in yml)
namespace My\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class User
{
...
And I created a child class that inherits from that entity, so that I can add some custom validation methods and a few fields that I need but do not need to be persisted (e.g. ConfirmPassword, ConfirmEmail fields)
namespace My\SecondBundle\EditModels;
use My\CoreBundle\Entity\User;
class UserModel extends User
{
When the user submit a registration form, I map the request to a UserModel entity, and if it is valid I try to persist the user.
The following code throws an exception
$entityManager->persist($userModel);
//=>The class 'My\SecondBundle\EditModels\UserModel' was not found in the chain configured namespaces My\CoreBundle\Entity
Question: How can I persist $userModel (instance of UserModel) as a User entity class? Possible options:
Do not use an inherited class and add custom fields and validation method to the User entity itself
Copy the fields from the UserModel to the User entity and persist the user entity
I don't think I should use Doctrine inheritance mechanism as I do not want to save the extra fields.
Thank you
I think your problem here, is that you've just configured My\CoreBundle\Entity namespace in Doctrine2, but the entity you actually want to persist is located in My\SecondBundle\EditModels.
Usually when inheriting classes marked as #ORM\Entity() the class you are extending from must have the class annotation #ORM\MappedSuperclass(). But normally you use this for single table inhertiance e.g., not for your usecase.
In my opinion the approach to split database related attributes from the others, is not affordable. I would keep validation related stuff in the model itself - you need it in your create/update action.
I'm not familiar with XML configuration, but when using annotations you need to mark each property to be mapped with database (using #ORM\Column()). So Doctrine will ignore all the other attributes and methods entirely.
So here I share my recently developed AbstractModel for you, to see how I've implemented validation (with respect/validation):
<?php
namespace Vendor\Package\Model;
use Doctrine\ORM\Mapping as ORM;
/**
* Abstract Model
*
* #ORM\MappedSuperclass()
*/
abstract class AbstractModel
{
/**
* #var \Respect\Validation\Validator
*/
protected $validator;
/**
* AbstractModel constructor
*/
public function __construct()
{
$this->validator = static::validation();
}
/**
* Defines validation for this model
*
* #return \Respect\Validation\Validator
*/
public static function validation() : \Respect\Validation\Validator
{
return \Respect\Validation\Validator::create();
}
/**
* Executes validations, defined in validation method.
*
* #return bool
*/
public function isValid() : bool
{
if (is_null($this->validator)) {
$this->validator = new \Respect\Validation\Validator();
$this->validation();
}
return $this->validator->validate($this);
}
}
A model which extends from the AbstractModel needs to implement a static validate method, to define class validation:
<?php
namespace Vendor\Package\Model;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
* #ORM\Table(name="my_model")
*/
class MyModel extends AbstractModel
{
/**
* #var string
* #ORM\Column(type="string")
*/
private $name;
/**
* Defines validation for this model
*
* #return \Respect\Validation\Validator
*/
public static function validation() : \Respect\Validation\Validator
{
return \Respect\Validation\Validator::create()
->attribute('name', \Respect\Validation\Validator::notEmpty()->stringType()->length(null, 32))
;
}
// getter, setter, ...
}
Each entity, persisted to database, will have the $validator property and all these methods, but because I left annotations here (and pretty sure this also works with xml/yaml) Doctrine ignores it.
And this way you also keep validation related stuff out of the model class itself, which is good for readability. The validation itself should be defined in the model itself, imho. But this respect/validation framework is neat way to achive this. Hope this helps :)

Laravel stop modal attribute being updated based on user role

I'm creating a system in which there are users, and user have many user roles. The user roles also contain permissions. Some fields are protected, so that they cannot be overwritten without the user possessing a specific permission.
For example, a user may have the attribute "email" which cannot be changed by the user, unless the user has the permission "update-email-address".
I originally intended to implement this concept as a trait or an abstract class, but I can't figure a way of doing this which doesn't involve either overloading the Eloquent Model constructor method, or else completely overloading another method.
What I'm hoping to do, is to be able to specify an array in a model like below, and by using a tract or extention, somehow prevent updating a model attribute:
/**
* The attributes that should only be updatable by given user with the
* specified permission
*
*/
public $update_only_by_permission = [
'email' => ['update-email-address'],
];
Is there a way to achieve this?
I stumbled across a way to provide a boot method for a trait extending a model, and was able to achieve this through the following:
Trait used on many Eloquent Models:
use Auth;
trait AttributeVisibleByPermissionTrait {
/**
* Stops updating of the object attributes if the authenticated
* user does not have the given permission provided in the
* $object->update_only_by_permission array.
*/
public static function bootAttributeVisibleByPermissionTrait(){
static::updating(function($object){
foreach ($object->update_only_by_permission as $attribute => $permissions) {
foreach ($permissions as $permission) {
if(!Auth::User()->can($permission)) {
unset($object->$attribute);
}
}
}
});
}
}
User Model:
class User extends Authenticatable
{
use AttributeVisibleByPermissionTrait;
/**
* The attributes that should only be updated by given user auth permissions
*
* #var array
*/
public $update_only_by_permission = [
'email' => ['update-email-address'],
];
}

UserProvider is not instantiable when extending Guard in Laravel

I made a custom guard implementation that force disables "remember me". Specifics of this custom implementation aside - when I try binding it to the Guard Contract it seems something goes wrong.
Customguard.php
<?php
namespace App\Contracts\Auth;
use Illuminate\Auth\Guard as StockGuard;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Contracts\Auth\Guard as GuardContract;
class CustomGuard extends StockGuard implements GuardContract
{
/**
* Log a user into the application.
*
* #param \Illuminate\Contracts\Auth\Authenticatable $user
* #param bool $remember
* #return void
*/
public function login (UserContract $user, $remember = false)
{
parent::login($user, false);
}
}
(Guard->login is overridden with exactly the same method signature)
I did the binding as follows, in the boot method of app/Providers/AuthServiceProvider.php
$this->app->bind('Illuminate\Contracts\Auth\Guard', App\Contracts\Auth\CustomGuard::class);
Everything works up until the point where CustomGuard->login is invoked.
BindingResolutionException in Container.php line 749:
Target [Illuminate\Contracts\Auth\UserProvider] is not instantiable.
What am I doing wrong?
Userprovider is an interface, you must use the class that implements the interface, I think that is eloquente Userprovidev.
Changed:
use Illuminate\Contracts\Auth\UserProvider;
To:
use App\Http\Controllers\Auth\CustomUserProvider as UserProvider;

Categories