Skip to content

Commit

Permalink
fix setting password
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathologic committed Jan 30, 2023
1 parent 9f5401a commit b2cd934
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Services/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public function changePassword($userData, bool $events = true, bool $cache = tru
return $user->process();
}

public function changeManagerPassword($userData, bool $events = true, bool $cache = true)
{
$user = new UserManagerChangePassword($userData, $events, $cache);
return $user->process();
}

public function hashChangePassword($userData, bool $events = true, bool $cache = true)
{
$user = new UserHashChangePassword($userData, $events, $cache);
Expand Down
130 changes: 130 additions & 0 deletions src/Services/Users/UserManagerChangePassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php namespace EvolutionCMS\UserManager\Services\Users;

use EvolutionCMS\Exceptions\ServiceActionException;
use EvolutionCMS\Exceptions\ServiceValidationException;
use EvolutionCMS\UserManager\Interfaces\UserServiceInterface;
use \EvolutionCMS\Models\User;
use Illuminate\Support\Facades\Lang;

class UserManagerChangePassword implements UserServiceInterface
{
/**
* @var \string[][]
*/
public $validate;

/**
* @var array
*/
public $messages;

/**
* @var array
*/
public $userData;

/**
* @var bool
*/
public $events;

/**
* @var bool
*/
public $cache;

/**
* @var array $validateErrors
*/
public $validateErrors;


/**
* UserRegistration constructor.
* @param array $userData
* @param bool $events
* @param bool $cache
*/
public function __construct(array $userData, bool $events = true, bool $cache = true)
{
$this->validate = $this->getValidationRules();
$this->messages = $this->getValidationMessages();
$this->userData = $userData;
$this->events = $events;
$this->cache = $cache;
}

/**
* @return \string[][]
*/
public function getValidationRules(): array
{
return [
'password' => ['required', 'min:6', 'confirmed'],
];
}

/**
* @return array
*/
public function getValidationMessages(): array
{
return [
'password.required' => Lang::get("global.required_field", ['field' => 'password']),
'password.confirmed' => Lang::get("global.password_confirmed", ['field' => 'password']),
'password.min' => Lang::get("global.password_gen_length"),

];
}

/**
* @return \Illuminate\Database\Eloquent\Model
* @throws ServiceActionException
* @throws ServiceValidationException
*/
public function process(): string
{
if (!$this->checkRules()) {
throw new ServiceActionException(\Lang::get('global.error_no_privileges'));
}

if (!$this->validate()) {
$exception = new ServiceValidationException();
$exception->setValidationErrors($this->validateErrors);
throw $exception;
}


$uid = EvolutionCMS()->getLoginUserID('mgr');
$password = EvolutionCMS()->getPasswordHash()->HashPassword($this->userData['password']);
$user = \EvolutionCMS\Models\User::find($uid);
$user->password = $password;
$user->save();

// invoke OnManagerChangePassword event
EvolutionCMS()->invokeEvent('OnManagerChangePassword', [
'userid' => $uid,
'username' => $_SESSION['mgrShortname'],
'userpassword' => $this->userData['password']
]);
return $user;
}

/**
* @return bool
*/
public function checkRules(): bool
{
return true;
}

/**
* @return bool
*/
public function validate(): bool
{
$validator = \Validator::make($this->userData, $this->validate, $this->messages);
$this->validateErrors = $validator->errors()->toArray();
return !$validator->fails();
}
}

0 comments on commit b2cd934

Please sign in to comment.