Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 36947b88

Přidáno uživatelem Petr Lukašík před téměř 6 roky(ů)

Re #7594 změna hesla a emailu pro uživatele

Zobrazit rozdíly:

app/FrontModule/component/User/SettingsForm.latte
1
<div class="row">
2
    <div class="col-4">
3
        {control form}
4
    </div>
5
</div>
app/FrontModule/component/User/SettingsForm.php
1
<?php
2

  
3

  
4
namespace App\FrontModule\Components;
5

  
6
use App\Enum\EFlashMessage;
7
use App\Model\Facade\UserFacade;
8
use App\Model\Repository\UserRepository;
9
use Nette\Application\UI\Control;
10
use App\Utils\Form;
11

  
12
class UserSettingsForm extends Control
13
{
14
    const PASSWORD_CONFIRM = 'password_confirm';
15

  
16
    /** @var Form  */
17
    private $form;
18

  
19
    /**
20
     * @var UserRepository
21
     */
22
    private $userRepository;
23

  
24
    /**
25
     * @var UserFacade
26
     */
27
    private $userFacade;
28

  
29
    public function __construct(UserRepository $userRepository,
30
                                UserFacade $userFacade
31
    )
32
    {
33
        parent::__construct();
34

  
35
        $this->form = new Form;
36
        $this->userRepository = $userRepository;
37
        $this->userFacade = $userFacade;
38
    }
39

  
40
    public function render()
41
    {
42
        $this->template->setFile(__DIR__ . '/SettingsForm.latte');
43
        $this->template->render();
44
    }
45

  
46
    public function createComponentForm()
47
    {
48
        $password = $this->form->addPassword(UserRepository::COLUMN_PASSWORD, 'New password')->setAttribute("placeholder", "Leave blank to remain unchanged");
49
        $this->form->addPassword(self::PASSWORD_CONFIRM, 'Confirm new password')
50
            ->addConditionOn($password, Form::FILLED, TRUE)
51
            ->addRule(Form::EQUAL, 'Passwords must match.', $password)
52
            ->addRule(Form::REQUIRED, 'Field %label is required')
53
            ->endCondition();
54

  
55
        $this->form->addText(UserRepository::COLUMN_EMAIL, 'E-mail')
56
            ->addRule(Form::EMAIL, 'Field %label must be valid e-mail.')
57
            ->setRequired(FALSE);
58

  
59
        $this->form->setDefaults($this->getDefaults());
60

  
61
        $this->form->addSubmit('submit', 'Save changes');
62

  
63
        $this->form->onSuccess[] = [$this, 'formSuccess'];
64

  
65
        return $this->form;
66
    }
67

  
68
    public function formSuccess(Form $form)
69
    {
70
        $result = $this->userFacade->saveUserSettings($form->getValues(true), $this->presenter->getUser()->getId());
71

  
72
        if ($result)
73
        {
74
            $this->presenter->flashMessage('Uživatel byl úspěšně upraven.', EFlashMessage::SUCCESS);
75
            $this->presenter->redirect('Homepage:userSettings');
76
        } else
77
        {
78
            $form->addError('Uživatele se nepodařilo upravit.');
79
        }
80
    }
81

  
82
    /**
83
     * Vrací hodnoty z databáze při upravování existujícího uživatele
84
     *
85
     * @return array
86
     */
87
    private function getDefaults()
88
    {
89
        $defaultValues = array();
90
        $userSelection = $this->userRepository->findById($this->presenter->getUser()->getId());
91
        $userRow = $userSelection->fetch();
92

  
93
        if ($userRow) {
94
            $defaultValues['email'] = $userRow->{UserRepository::COLUMN_EMAIL};
95
        }
96

  
97
        return $defaultValues;
98
    }
99
}
100

  
101
interface IUserSettingsFormFactory
102
{
103
    /**
104
     * @return UserSettingsForm
105
     */
106
    public function create();
107
}
app/FrontModule/presenters/HomepagePresenter.php
2 2

  
3 3
namespace App\FrontModule\Presenters;
4 4

  
5
use App\FrontModule\Components\IUserSettingsFormFactory;
5 6
use App\FrontModule\Components\IExampleGirdFactory;
6 7
use App\FrontModule\Components\ILoginFormFactory;
7 8
use App\Enum\EFlashMessage;
......
15 16
    /** @var ILoginFormFactory */
16 17
    private $loginFormFactory;
17 18

  
18
    public function __construct(IExampleGirdFactory $exampleGridFactory, ILoginFormFactory $loginFormFactory)
19
    /** @var IUserSettingsFormFactory */
20
    private $userSettingsFormFactory;
21

  
22
    public function __construct(IExampleGirdFactory $exampleGridFactory,
23
                                ILoginFormFactory $loginFormFactory,
24
                                IUserSettingsFormFactory $userSettingsFormFactory
25
    )
19 26
    {
20 27
        parent::__construct();
21 28

  
22 29
        $this->exampleGridFactory = $exampleGridFactory;
23 30
        $this->loginFormFactory = $loginFormFactory;
31
        $this->userSettingsFormFactory = $userSettingsFormFactory;
24 32
    }
25 33

  
26 34
    public function actionDefault()
......
28 36

  
29 37
    }
30 38

  
39
    public function actionUserSettings()
40
    {
41
        if (!$this->getUser()->isLoggedIn())
42
        {
43
            $this->redirect('Homepage:default');
44
        }
45
    }
46

  
31 47
    public function actionLogin()
32 48
    {
33 49
        if ($this->user->isLoggedIn())
......
57 73
        return $this->loginFormFactory->create();
58 74
    }
59 75

  
76
    /**
77
     * @return \App\FrontModule\Components\UserSettingsForm
78
     */
79
    public function createComponentUserSettingsForm(){
80
        return $this->userSettingsFormFactory->create();
81
    }
82

  
60 83
    /**
61 84
     * Vytvoření ukázkového gridu
62 85
     */
app/FrontModule/templates/Homepage/userSettings.latte
1
{block content}
2
    <div class="row">
3
        <div class="col">
4
            <fieldset>
5
                <legend>Change user email or password</legend>
6

  
7
                {control userSettingsForm}
8
            </fieldset>
9

  
10
        </div>
11
    </div>
12
{/block}
app/config/components.neon
26 26
    - App\AdminModule\Components\IExportFormFactory
27 27

  
28 28
    - App\FrontModule\Components\IKeyboard
29
    - App\FrontModule\Components\ITransliterationSearchResultListFactory
29
    - App\FrontModule\Components\ITransliterationSearchResultListFactory
30
    - App\FrontModule\Components\IUserSettingsFormFactory
app/model/facade/UserFacade.php
72 72
        return $userRow;
73 73
    }
74 74

  
75
    public function saveUserSettings(array $values, int $id){
76
        unset($values[UserEditForm::PASSWORD_CONFIRM]);
77

  
78

  
79
        if (empty($values[UserRepository::COLUMN_PASSWORD]))
80
        {
81
            unset($values[UserRepository::COLUMN_PASSWORD]);
82
        } else
83
        {
84
            $values[UserRepository::COLUMN_PASSWORD] = md5($values[UserRepository::COLUMN_PASSWORD]);
85
        }
86

  
87
        $userRow = $this->userRepository->findById($id)->fetch();
88

  
89
        if ($userRow) {
90
            $userRow->update($values);
91
        }
92

  
93
        return $userRow;
94
    }
95

  
75 96
    /**
76 97
     * Odstraní uživatele
77 98
     *
app/templates/@menu.latte
32 32
    <div class="navbar-nav nav-item">
33 33
        {if $user->loggedIn}
34 34
            <a class="nav-link" n:href=":Front:Homepage:logout">Logout</a>
35
            <a class="nav-link" href="#">Change Password</a>
35
            <a class="nav-link" n:href=":Front:Homepage:userSettings">User settings</a>
36 36
        {else}
37 37
            <a class="nav-link" n:href=":Front:Homepage:login">Login</a>
38 38
        {/if}

Také k dispozici: Unified diff