統計
| ブランチ: | リビジョン:

pictcode / app / Controller / UsersController.php @ a5ebb280

履歴 | 表示 | アノテート | ダウンロード (5.437 KB)

1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Users Controller
5
 *
6
 * @property User $User
7
 * @property PaginatorComponent $Paginator
8
 */
9
class UsersController extends AppController {
10

    
11
        public $layout = 'procedure';
12

    
13
    public function beforeFilter() {
14
        parent::beforeFilter();
15
        $this->Auth->allow('register','activate');
16
    }
17

    
18
/**
19
 * Components
20
 *
21
 * @var array
22
 */
23
        public $components = array('Paginator');
24

    
25
/**
26
 * index method
27
 *
28
 * @return void
29
 */
30
        public function index() {
31
                $this->User->recursive = 0;
32
                $this->set('users', $this->Paginator->paginate());
33
        }
34

    
35
/**
36
 * view method
37
 *
38
 * @throws NotFoundException
39
 * @param string $id
40
 * @return void
41
 */
42
        public function view($id = null) {
43
                if (!$this->User->exists($id)) {
44
                        throw new NotFoundException(__('Invalid user'));
45
                }
46
                $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
47
                $this->set('user', $this->User->find('first', $options));
48
        }
49

    
50

    
51
/**
52
 * register method
53
 *
54
 * @return void
55
 */
56
        public function register() {
57
                if($this->request->is('post') || $this->request->is('put')){
58
                        $this->User->set($this->request->data);
59
                        if($this->User->validates()){
60
                                $this->Session->write('register',$this->request->data);
61
                                $this->redirect(array('action'=>'confirm'));
62
                        }else{
63
                        }
64
                }
65
        }
66
        
67
/**
68
 * register confirm
69
 */
70
        public function confirm() {
71
                if($this->Session->read('register')){
72
                        $this->set('register',$this->Session->read('register'));
73
                }else{
74
                        $this->redirect(array('action'=>'register'));
75
                }
76
        }
77

    
78

    
79
/**
80
 * register sent
81
 */
82
        public function sent() {
83
                // if (!empty( $this->data)){
84
         //        //  保存
85
           if( $this->User->save($this->Session->read('register'))){
86
                    //  メール送信
87
                $this->set('register',$this->Session->read('register'));
88
                $name = $this->Session->read('register.User.login_id');
89
                $mail = $this->Session->read('register.User.email');
90
        // ユーザアクティベート(本登録)用URLの作成
91
        $url = 
92
            DS . 'users' .          // コントローラ
93
            DS . 'activate' .                       // アクション
94
            DS . $this->User->id .                  // ユーザID
95
            DS . $this->User->getActivationHash();  // ハッシュ値
96
        $url = Router::url( $url, true);  // ドメイン(+サブディレクトリ)を付与
97
                $comment = $url;
98

    
99
                $Email = new CakeEmail();
100
                $Email->charset('ISO-2022-JP');
101
                $Email->emailFormat('text');
102
                $Email->template('user_register');
103
                $Email->viewVars(array('name'=>$name,'comment'=>$comment));
104
                $Email->from($mail);
105
                $Email->to('hasegawa@i-hearts.jp');
106
                $Email->subject('[PICT CODE]問い合わせ');
107
                $Email->send();
108
            }
109

    
110
        }        
111

    
112
/**
113
 * register activate
114
 */
115
        public function activate( $user_id = null, $in_hash = null) {
116
            // UserモデルにIDをセット
117
            $this->User->id = $user_id;
118
            if ($this->User->exists() && $in_hash == $this->User->getActivationHash()) {
119
            // 本登録に有効なURL
120
                // statusフィールドを0に更新
121
                $this->User->saveField( 'status', 0);
122
                $this->Session->setFlash( 'Your account has been activated.');
123
            }else{
124
            // 本登録に無効なURL
125
                $this->Session->setFlash( 'Invalid activation URL');
126
            }
127
        }
128
        
129
/**
130
 * add method
131
 *
132
 * @return void
133
 */
134
        public function add() {
135
                if ($this->request->is('post')) {
136
                        $this->User->create();
137
                        if ($this->User->save($this->request->data)) {
138
                                $this->Flash->success(__('The user has been saved.'));
139
                                return $this->redirect(array('action' => 'index'));
140
                        } else {
141
                                $this->Flash->error(__('The user could not be saved. Please, try again.'));
142
                        }
143
                }
144
        }
145

    
146
/**
147
 * edit method
148
 *
149
 * @throws NotFoundException
150
 * @param string $id
151
 * @return void
152
 */
153
        public function edit($id = null) {
154
                if (!$this->User->exists($id)) {
155
                        throw new NotFoundException(__('Invalid user'));
156
                }
157
                if ($this->request->is(array('post', 'put'))) {
158
                        if ($this->User->save($this->request->data)) {
159
                                $this->Flash->success(__('The user has been saved.'));
160
                                return $this->redirect(array('action' => 'index'));
161
                        } else {
162
                                $this->Flash->error(__('The user could not be saved. Please, try again.'));
163
                        }
164
                } else {
165
                        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
166
                        $this->request->data = $this->User->find('first', $options);
167
                }
168
        }
169

    
170
/**
171
 * delete method
172
 *
173
 * @throws NotFoundException
174
 * @param string $id
175
 * @return void
176
 */
177
        public function delete($id = null) {
178
                $this->User->id = $id;
179
                if (!$this->User->exists()) {
180
                        throw new NotFoundException(__('Invalid user'));
181
                }
182
                $this->request->allowMethod('post', 'delete');
183
                if ($this->User->delete()) {
184
                        $this->Flash->success(__('The user has been deleted.'));
185
                } else {
186
                        $this->Flash->error(__('The user could not be deleted. Please, try again.'));
187
                }
188
                return $this->redirect(array('action' => 'index'));
189
        }
190

    
191
/**
192
 * login method
193
 *
194
 * @throws NotFoundException
195
 * @param string $id
196
 * @return void
197
 */
198
        public function login() {
199
                //var_dump(Security::hash( "123", 'blowfish'));
200
                if($this->Auth->user()){
201
                        $this->redirect($this->Auth->redirectUrl());                          
202
                }
203
                if ($this->request->is('post')) {
204
                        if ($this->Auth->login()) {
205
                                $this->redirect($this->Auth->redirectUrl());                          
206
                        } else {
207
                                $this->Flash->error(__('login error'));
208
                        }
209
                }
210
        }        
211
/**
212
 * logout method
213
 *
214
 * @throws NotFoundException
215
 * @param string $id
216
 * @return void
217
 */
218
        public function logout() {
219
            $this->redirect($this->Auth->logout());
220
        }
221

    
222

    
223

    
224
}
225

    
226