Ngu ngơ học làm web (x32) - CakePHP2 - getUser, chỉ thực hiện khi đã đăng nhập

Tiếp theo của: Ngu ngơ học làm web (x31) - CakePHP2 – beforeFilter, lấy thông tin user, virtualFields
-----

Phần x32. CakePHP2 – getUser, chỉ thực hiện khi đã đăng nhập


Xem (clip số 34b – chickenrainshop):

- Ràng buộc login trên các form

Chỉ hiển thị ô ‘gửi nhận xét’ nếu người dùng đã đăng nhập:

[View\Books\view.ctp]

<p class="comment">
<strong><?php echo $comment['User']['fullname']; ?>:</strong>
<?php echo $comment['Comment']['content']; ?>
</p>
<!-- gửi nhận xét -->
<h4>Gửi nhận xét</h4>
<?php if (!empty($userInfo)): ?>
            <?php echo $this->element('errors'); ?>
            <?php echo $this->Flash->render(); ?>
            <?php echo $this->Form->create('Comment', ['url' => ['action' => 'add'], 'novalidate' => true, 'class' => 'comments form']); ?>
            <?php
            // bỏ trường này đi để đảm bảo an toàn thông tin
            // echo $this->Form->input('user_id',
            //          [
            //          'type' => 'text',
            //          'value' => 1,
            //          'hidden' => true,
            //          'label' => '',
            //          'required' => false
            //          ]);
            echo $this->Form->input('book_id',
                        [
                        'type' => 'text',
                        'hidden' => true,
                        'label' => '',
                        'required' => false,
                        'value' => $book['Book']['id']
                        ]);
            echo $this->Form->input('content', ['label' => '', 'rows' => 5, 'class' => 'col-lg-12']);
            ?>
            <?php echo $this->Form->button('Gửi', ['type' => 'submit', 'class' => 'pull-right btn btn-primary col-lg-3 send-button']); ?>
            <?php echo $this->Form->end(); ?>
<?php else: ?>
            Bạn phải <?php echo $this->Html->link('đăng nhập', '/login'); ?> trước khi đánh giá.
<?php endif ?>

Ở đoạn mã trên đã xóa bỏ thông tin về user_id, do vậy trong [action add() Controller\CommentsController.php] không thể lấy giá trị user_id từ view, mà sẽ gán giá trị cho user_id tại controller:

[action add() trong Controller\CommentsController.php]

public function add() {
                        if ($this->request->is('post')) {
                                    $this->Comment->set($this->request->data);
                                    if ($this->Comment->validates()) {
                                                $this->Comment->create();
                                                $userInfo = $this->getUser();
                                                $this->request->data['Comment']['user_id'] = $userInfo['id'];
                                                if ($this->Comment->save($this->request->data)) {


Sửa lỗi liên quan đến biến ‘fullname’:

[action view() trong BooksController.php]

$this->loadModel('Comment');
                        $comments = $this->Comment->find('all', [
                                                'conditions' => [
                                                            'book_id' => $book['Book']['id']
                                                            ],
                                                'order' => ['Comment.created' => 'asc'],
                                                'contain' => ['User' => ['fullname']]

                                    ]);
                        $this->set('comments', $comments);

Chỉ hiển thị thông tin về thanh toán, nếu người dùng đã đăng nhập:

[View\Books\view_cart.ctp]

            <!-- customer info -->
            <div class="panel panel-info col col-lg-7 col-lg-offset-1">
                        <h4 class="panel-heading"><span class="glyphicon glyphicon-user"></span>Thanh toán đơn hàng</h4>
                        <?php //if (true): ?>
                        <?php if (!empty($userInfo)): ?>
                                    <?php echo $this->Flash->render('order'); ?>
                                    <?php echo $this->Form->create('Order', ['url' => ['action' => 'checkout'], 'class' => 'form-horizontal', 'inputDefaults' => ['label' => false]]); ?>
                                    <div class="row">
                                                <?php echo $this->Form->label('name', 'Tên', ['class' => 'col col-lg-3 control-label']); ?>
                                                <div class="col col-lg-9">
                                                            <?php echo $this->Form->input('name', ['placeholder' => 'Nhập tên', 'value' => $userInfo['fullname']]); ?>
                                                </div>
                                    </div>
                                    <div class="row">
                                                <?php echo $this->Form->label('email', 'Email', ['class' => 'col col-lg-3 control-label']); ?>
                                                <div class="col col-lg-9">
                                                            <?php echo $this->Form->input('email', ['placeholder' => 'Nhập email', 'value' => $userInfo['email']]); ?>
                                                </div>
                                    </div>
                                    <div class="row">
                                                <?php echo $this->Form->label('address', 'Địa chỉ', ['class' => 'col col-lg-3 control-label']); ?>
                                                <div class="col col-lg-9">
                                                            <?php echo $this->Form->input('address', ['placeholder' => 'Nhập địa chỉ', 'value' => $userInfo['address']]); ?>
                                                </div>
                                    </div>
                                    <div class="row">
                                                <?php echo $this->Form->label('phone', 'Điện thoại', ['class' => 'col col-lg-3 control-label']); ?>
                                                <div class="col col-lg-9">
                                                            <?php echo $this->Form->input('phone', ['placeholder' => 'Nhập số điện thoại', 'value' => $userInfo['phone_number']]); ?>
                                                </div>
                                    </div>
                                    <div class="row">
                                                <div class="col col-lg-10 col-offset-2">
                                                            <?php echo $this->Form->button('Thực hiện thanh toán', ['type' => 'submit', 'class' => 'btn btn-primary pull-right']) ?>
                                                </div>
                                    </div>
                        <?php echo $this->Form->end(); ?>
                        <?php else: ?>
                                    Bạn phải đăng nhập trước khi thanh toán.
                        <?php endif ?>
            </div>
            <!-- end customer info -->

Chỉnh sửa cho action checkout() trong OrdersController.php:

public function checkout() {
                        if ($this->request->is('post')) {
                                    $userInfo = $this->getUser();
                                    $data = [
                                                'user_id' => $userInfo['id'],
                                                'order_info' => json_encode($this->Session->read('cart')),


Chỉnh sửa cho view Users\login.ctp:

<div class="panel panel-info">
            <h4 class="panel-heading"><span class="glyphicon glyphicon-user"></span></h4>
            <?php if (empty($userInfo)): ?>
                        <?php echo $this->Flash->render('auth'); ?>
                        <?php echo $this->Form->create('User', ['class' => 'form-horizontal', 'novalidate' => true, 'inputDefaults' => ['label' => false]]); ?>
                                    <div class="control-group">
                                                <label class="control-label" for="inputUsername">Username</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('username', ['placeholder' => 'Tên đăng nhập']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <label class="control-label" for="inputPassword">Password</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('password', ['placeholder' => 'Mật khẩu']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <div class="controls">
                                                            <?php echo $this->Form->button('Đăng nhập', ['type' => 'submit', 'class' => 'col-lg-2 btn btn-primary']); ?>
                                                </div>
                                    </div>
                        <?php echo $this->Form->end(); ?>
            <?php else: ?>
                        Bạn đã đăng nhập, bấm vào <?php echo $this->Html->link('đây', '/'); ?> để quay về trang chủ.
            <?php endif ?>

</div>
-----------
Cập nhật 23/6/2017
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web