How to add hash password to database? - php

I have store() in controller, and in this place I call 3 functions. Variable $users store correct fields validation with values them. In next step I call function which hash password. At the end I call function which add user to database. My problem is that hash password is not add to database.What can I do that pass hashPassword to createUser() ?
public function store(RegistrationRequest $request, UserService $user)
{
$user = $request->correctValidate();
$userService->hashPassword($user['password']);
$userService->createUser($user);
}
public function createUser($request): void
{
User::create($request);
}
public function hashPassword($request)
{
return Hash::make($request);
}
public function correctValidate()
{
return $this->validated();
}

#Deks2 Please try below solution:
$user['password'] = $userService->hashPassword($user['password']);

Related

Too few arguments to function App\Http\Controllers\UsersController::edit(), 1 passed and exactly 2 expected

Too few arguments to function App\Http\Controllers\UsersController::edit(), 1 passed and exactly 2 expected
public function edit ($id , User $user) {
$user = $user->find($id);
return view('admin.user.edit',compact('user'));
}
If you url is something like: site.com/profile/1
And if this url corespond the this edit function
// you can just ask from your model to get the user that has id: 1
// which is coming from Eloquent model.
public function edit (User $user) {
return view('admin.user.edit',compact('user'));
}
If your url is not like this also function wouldn't work tho
You should edit you method to the following:
public function edit (int $id , UserRepository $repo) {
$user = $repo->find($id);
return view('admin.user.edit', compact('user'));
}
And make sure at the top of your file you have appropriate namespaces declared.

Laravel 5.8 send parameters to authorize method FormRequest Class

I have update and store method like this
public function update(ContactRequest $request)
{
if (Auth::user()->can('edit_contact'))
$request->update();
else
return $this->accessDenied();
}
public function store(ContactRequest $request)
{
if (Auth::user()->can('add_contact'))
$request->store();
else
return $this->accessDenied();
}
and authorize in FormRequest class
public function authorize()
{
return \Gate::allows('test', $this->route('contact'));
}
I want to pass permission name to authorize method like this:
public function authorize($permissionName)
{
if (Auth::user()->can($permissionName))
return \Gate::allows('test', $this->route('contact'));
}
and in controller like this
public function update(ContactRequest $request)
{
$request->update('edit_contact');
}
public function store(ContactRequest $request)
{
$request->store('add_contact');
}
You have 3 options:
Change your authorization method to this:
public function authorize()
{
return $this->user()->can(
$this->route()->getActionMethod() === 'store'
? 'add_contact'
: 'edit_contact'
)
&& \Gate::allows('test', $this->route('contact'));
}
Make your authorize method of request return true and check authorization by defining another gate an call it on your controller:
public function authorize()
{
return true;
}
Gate::define('modify_contact', function ($user, $permissionName) {
return $user->can($permissionName)
&& $user->can('test', $request->route('contact'));
});
public function update(ContactRequest $request)
{
Gate::authorize('modify_contact', 'edit_contact');
//...
}
public function store(ContactRequest $request)
{
Gate::authorize('modify_contact', 'add_contact');
//...
}
Define and use policy the same way and pass your arguments to it.
There is no direct way of passing argument to authorize method of form request, but you can do the implementation this way:
public function authorize()
{
$method = Request::method();
if($method == 'post') {
$permission = 'add_contact';
} elseif($method == 'put') {
$permission = 'edit_contact';
}
if (Auth::user()->can($permission))
return \Gate::allows('test', $this->route('contact'));
}
If you are using laravel's default post, put routes then this will help you out.
It is better to make two different Requests for store and update, anyway you need to check some values depended on action.
So you can user default laravel's policy approach for Resource controllers and not use Request::authorize for authorization logic.
Laravel policy controller helpers

Arguement 1 must be instance of Illuminate\Http\Request integer given

When I am trying to send mail, everytime a new member is added to the user table, so that they can get a setup password link. I have been trying to get this to work but seem not to be.
public function store(AddUser $request)
{
$user = $request->all();
$user['activate'] = $this->active();
$user['guid'] = $this->guid();
$user['accountno'] = $this->generateAndValidateAccountno();
$check = User::find($user['phone']);
if(!$check) {
$id = User::create($user);
$this->sendEmail($user['accountno']);
}
return redirect('employee');
}
public function sendEmail(Request $request, $id)
{
$user = User::find($id);
Beautymail::send('emails.welcome', [], function($message)
{
$message
->to('$id->email', '$id->fname')
->subject('Welcome!');
});
}
}
Not sure what am doing wrong
Just use the same request class in the controller and the model. In your user model, add use Illuminate\Http\Request at the top of the class to tell it which Request class to use.
Just change:
public function sendEmail(Request $request, $id){...}
to
public function sendEmail($id){...}

Symfony2 modify global user reference?

I have functions setAttribute($key, $value) and getAttribute($key, $default) in my User class. When the user is authenticated I want to set several attributes to be set that will be used later in various controllers.
I tried setting the attributes in my success handler function:
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$token->getUser()->setAttribute("user_data_set", 1);
}
But when I tried calling it in my controller the value has not been set
public function indexAction(Request $request) {
//Get the logged in user
$user = $this->getUser();
//Entity Manager
$em = $this->getDoctrine()->getManager();
// this page is just used as the starting point to redirect the user to the appropriate page
if($user->getAttribute("user_data_set", 0) == 1)
{
//Get Symfony1 route from the user data table
$old_homepage = $user->getAttribute("user_homepage", "#default_homepage");
//Convert route to Symfony2 format
$new_homepage = $this->setForwardingAddress($old_homepage);
return $this->redirect($this->generateUrl($new_homepage));
}
else
{
return $this->redirect('login');
}
}
How can I modify the global user instead of a local reference?
You obviously want cross-request solution so you'll need to use DB or session.
For example:
// Injecting Session service
public function __construct(Session $session){
$this->session = $session
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$this->session->set('user_data_set', 1);
}
And then in your controller:
$foo = $session->get('user_data_set');
Is this what you wanted?

Laravel passing username parameter to controller from user.blade.php

Hi on my website I have profile with username parameter root/user/{username}. I was planning to add button to block the user. My problem is that when other user click block button, the button do stuffs in the Check.php controller but it doesn't pass the user/{username} parameter that I need in if statements. My question is how I can pass the {username} parameter from my user.blade.php to the Check.php controller?
As we are not seeing any code here, it seems that to do what you need, you just have to create a route pointing to your controller method and eventually a route to redirect your users when successufully blocked:
Route::get('user/block/{username}', 'BlockUserController#block');
Route::get('userBlocked', 'BlockUserController#blocked');
And the controller itself:
class BlockUserController extends Controller {
public function block($username)
{
$user = User::where('username', $username);
$user->blocked = true;
$user->save();
return Redirect::to('userBlocked');
}
public function blocked($username)
{
return View::make('user.blocked');
}
}
And then if you click the button pointing to the route:
http://application.com/user/block/user3398940
It will be blocked.
If you want to go a little more advanced in Laravel, you can use dependency injection and remove some code from your controller:
class BlockUserController extends Controller {
private $user;
public function __construct(User $user)
$this->user = $user;
}
public function block($username)
{
if ($user->block($username))
{
return Redirect::to('userBlocked');
}
return Redirect::back()->with('message', 'User not found');
}
public function blocked($username)
{
return View::make('user.blocked');
}
}
And your user model would have to have a block method:
class User extends Eloquent {
public function block($username)
{
if ($user = $this->newQuery()->where('username', $username))
{
$user->blocked = true;
return $user->save();
}
return false;
}
}

Categories