New Password method

Admin action to change someones password.

@param string|null $id User id.
@return void|\Cake\Network\Respose
334841
public function newPassword($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => []
        ]);

        $user->accessible('send_mail', true); // checkbox to send an e-mail

        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                if ($user->send_mail) {
                    $this->EmailListener->passwordConfirmation($user);
                }
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect($this->referer());
            } else {
                $this->Flash->error(__('The password could not be saved. Please, try again.'));
            }
        }

        $this->set(compact('user'));

        $this->render(Configure::read('CM.AdminUserViews.newPassword'));
    }
1136390
public function reset($id)
    {
        $password = Str::random();

        $input = [
            'password' => $password,
        ];

        $rules = [
            'password' => 'required|min:6',
        ];

        $val = UserRepository::validate($input, $rules, true);
        if ($val->fails()) {
            return Redirect::route('users.show', ['users' => $id])->withErrors($val->errors());
        }

        $user = UserRepository::find($id);
        $this->checkUser($user);

        $user->update($input);

        $mail = [
            'password' => $password,
            'email'    => $user->getLogin(),
            'subject'  => Config::get('app.name').' - New Password Information',
        ];

        Mail::queue('credentials::emails.password', $mail, function ($message) use ($mail) {
            $message->to($mail['email'])->subject($mail['subject']);
        });

        return Redirect::route('users.show', ['users' => $id])
            ->with('success', 'The user\'s password has been reset successfully, and has been emailed to them.');
    }
166710
public function setId($id)
    {
        if ($this->id) {
            return;
        }
        if (strlen($id) >= 20) {
            $this->id = $id;

            return;
        }
        list($a, $b) = explode(' ', microtime());
        $this->id = preg_replace("/[^\d]/", '', $b.$a.$id);
    }
240434
public function changePasswordAction()
    {
        if ($this->changePasswordSnippets) {
            $params = $this->_processParameters($this->changePasswordParameters);

            $this->addSnippets($this->changePasswordSnippets, $params);
        }
    }
1208207
public function iHaveUserWithId( $id )
    {
        $name = $this->findNonExistingUserName();
        $user = $this->getUserManager()->ensureUserExists( $name );
        $this->addValuesToKeyMap( $id, $user->id );
    }
1321754
public function edit($id = null)
    {
        $tag = $this->PostsTags->Tags->get($id);

        if ($this->request->is(['patch', 'post', 'put'])) {
            $tag = $this->PostsTags->Tags->patchEntity($tag, $this->request->getData());

            if ($this->PostsTags->Tags->save($tag)) {
                $this->Flash->success(I18N_OPERATION_OK);

                return $this->redirect(['action' => 'index']);
            }

            $this->Flash->error(I18N_OPERATION_NOT_OK);
        }

        $this->set(compact('tag'));
    }
999564
public function passwordResetRequest($id)
    {
        if (! $user = $this->user->find($id)) {
            flash(trans('core::messages.resource.resource not found', ['name' => trans('user::users.title.user')]));

            return redirect()->route('backend::user.index');
        }

        $code = $this->auth->createReminderCode($user);

        event(new UserHasBegunResetProcess($user, $code));

        Flash::success(trans('user::messages.reset password email sent'));

        return redirect()->route('backend::user.user.index');
    }
989573
public function setupPassword($id = null, $token = null)
    {
        $user = $this->_getUser($id, $token);

        if ($user === null) {
            $this->Flash->error(__d('community', 'User was not found'));
            return $this->redirect(['action' => 'login']);
        }

        if ($this->request->is(['patch', 'post', 'put'])) {
            $entity = $this->Users->patchEntity($user, $this->request->getData());
            if ($user->status) {
                $entity->set('token', null);
            }

            /** @var User $result */
            $result = $this->Users->save($user);
            if ($result) {
                $this->Flash->success(__d(
                    'community',
                    '«{0}», You have successfully changed your password.',
                    sprintf('<strong>%s</strong>', $user->get('name'))
                ));

                if (!$user->status && $result->token !== null) {
                    return $this->redirect([
                        'action' => 'activate',
                        $user->id,
                        $user->token
                    ]);
                }

                return $this->redirect(['action' => 'login']);
            } else {
                $this->Flash->error(__d('community', 'An error has occurred. Please, try again.'));
            }
        }

        $this
            ->set('user', $user)
            ->set('page_title', __d('community', 'Setup new password'));
    }
1299057
public function setId($id = null)
    {
        $this->id = ($id !== null) ? $id : SecureKey::generate();

        return $this;
    }
518959
public function setId(string $id = null): ButtonInterface
    {
        if ($this->isVisibility()) {
            $this->id = $id;
        }

        return $this;
    }
1042493
public function delete(string $id = null): void
    {
        $this->request->allowMethod(['post', 'delete']);
        $menu = $this->Menus->get($id);
        if ($this->Menus->delete($menu)) {
            $this->Flash->success((string)__('The menu has been deleted.'));
        } else {
            $this->Flash->error((string)__('The menu could not be deleted. Please, try again.'));
        }

        $this->redirect(['action' => 'index']);
    }
908636
public function edit($id = null) {
		$block = $this->Blocks->get($id, [
			'contain' => []
		]);
		if ($this->request->is(['patch', 'post', 'put'])) {
			$block = $this->Blocks->patchEntity($block, $this->request->data);
			if ($this->Blocks->save($block)) {
				$this->Flash->success('The block has been saved.');
				return $this->redirect(['action' => 'index']);
			} else {
				$this->Flash->error('The block could not be saved. Please, try again.');
			}
		}
		$this->set(compact('block'));
	}
84838
public function delete(string $id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $data = $this->request->getData();
        $permission = $this->Permissions->get($id);
        if ($this->Permissions->delete($permission)) {
            $this->Flash->success((string)__('The  permission has been deleted.'));
        } else {
            $this->Flash->error((string)__('The  permission could not be deleted. Please, try again.'));
        }

        $this->redirect($this->referer());
    }
609028
public function delete($id = null) {
        $user = $this->Users->get($id);
        if ($this->request->is(['patch', 'post', 'put'])) {
            if ($this->Users->delete($user)) {
                $this->Flash->success(__('The user has been deleted.'));
            } else {
                $this->Flash->error(__('The user could not be deleted. Please, try again.'));
            }
            return $this->redirect(['action' => 'index']);
        }
        $this->set(compact('user'));
    }
324547
public function delete($id = null)
    {
        $user = $this->Users->get($id);
        $this->request->allowMethod(['post', 'delete']);

        if ($this->Users->delete($user)) {
            $this->Flash->success(__('The user has been deleted.'));
        } else {
            $this->Flash->error(__('The user could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
978606
public function update($id)
    {
        $success = false;
        $message = '';
        $user    = null;
        $code    = 0;

        try{
            $input = Input::put();
            /** in case request come from post http form */
            $input = is_null($input) ? Input::post() : $input;

            if($input['password'] != $input['confirm_password']){
                throw new Exception("Password and confirmation password not match", 1);
            }

            $user = Sentry::findUserById($id);

            $user->email        = $input['email'];
            $user->first_name   = $input['first_name'];
            $user->last_name    = $input['last_name'];

            if($input['password']){
                $user->password = $input['password'];
            }

            $success = $user->save();
            $code    = 200;
            $message = 'User updated sucessully';
        }catch(UserNotFoundException $e){
            $message = $e->getMessage();
            $code    = 404;
        }catch (Exception $e){
            $message = $e->getMessage();
            $code    = 500;
        }

        if(Request::isAjax()){
            Response::headers()->set('Content-Type', 'application/json');
            Response::setBody(json_encode(
                array(
                    'success'   => $success,
                    'data'      => ($user) ? $user->toArray() : $user,
                    'message'   => $message,
                    'code'      => $code
                )
            ));
        }else{
            Response::redirect($this->siteUrl('admin/user/'.$id.'/edit'));
        }
    }
383693
public function edit($id)
    {
        $this->loadModel('User.Users');
        $user = $this->Users->get($id, ['contain' => ['Roles']]);
        $languages = LocaleToolbox::languagesList();
        $roles = $this->Users->Roles->find('list', [
            'conditions' => [
                'id NOT IN' => [ROLE_ID_AUTHENTICATED, ROLE_ID_ANONYMOUS]
            ]
        ]);

        if ($this->request->data()) {
            $user->accessible(['id', 'username'], false);
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                $this->Flash->success(__d('user', 'User information successfully updated!'));
                $this->redirect($this->referer());
            } else {
                $this->Flash->danger(__d('user', 'User information could not be saved, please check your information.'));
            }
        }

        $this->title(__d('user', 'Editing User'));
        $this->set(compact('user', 'roles', 'languages'));
        $this->Breadcrumb->push('/admin/user/manage');
    }