Commit 5e93d833 by Teeradone-PIM

teeradone: register

parent 2905751c
<?php <?php
namespace App\Controller; namespace App\Controller;
use App\Controller\AppController; use App\Controller\AppController;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Http\Client; use Cake\Http\Client;
use Cake\Routing\Router; use Cake\Routing\Router;
use Cake\I18n\Time;
use Cake\Mailer\Email;
use Cake\Utility\Security;
class UsersController extends AppController class UsersController extends AppController
{ {
...@@ -86,46 +89,178 @@ class UsersController extends AppController ...@@ -86,46 +89,178 @@ class UsersController extends AppController
public function signup() public function signup()
{ {
$this->loadModel('MasterCountries');
$this->loadModel('MasterProvinces');
$countries = $this->MasterCountries->find('list',
[
'conditions' => ['is_used' => true,'id'=>1],
'keyField' => 'id',
'valueField' => ['country_name_th']
]
);
if(!empty($countries)) $countries = $countries->toArray();
$provinces = $this->MasterProvinces->find('list',
[
'conditions' => ['is_used' => true,'master_country_id'=>1],
'keyField' => 'id',
'valueField' => 'province_name_th'
]
);
if(!empty($provinces)) $provinces = $provinces->toArray();
$this->set(compact('provinces','countries'));
$this->viewBuilder()->layout('blank'); $this->viewBuilder()->layout('blank');
} }
// public function getProvince() {
// $this->autoRender = false;
// $this->loadModel('MasterProvinces');
// $states = array();
// pr($this->request->$data);die;
// if (isset($this->request['data']['id'])) {
// $states = $this->MasterProvinces->find('list', array(
// 'keyField' => 'id',
// 'valueField' => ['country_name_th'],
// 'conditions' => array(
// 'State.countries_id' => $this->request['data']['id']
// )
// ));
// }
// header('Content-Type: application/json');
// pr($states);die;
// echo json_encode($states);
// exit();
// }
#--------------------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------
#Signup #Signup
public function createAccount() public function createAccount()
{ {
$this->viewBuilder()->layout('blank'); $this->loadModel('UserPersonals');
$data = $this->request->data(); $users = $this->Users->newEntity();
$user_personals = $this->UserPersonals->newEntity();
if ($this->request->is('post')) {
// $username_check = $this->MasterProvinces->find('all')->toArray();
// pr($username_check);die;
// pr($this->request->data);die;
$today = Time::now();
$dateNow = $today->i18nFormat('yyyy-MM-dd HH:mm:ss');
$hasher = new DefaultPasswordHasher();
$firstname = $this->request->data['firstname'];
// $phone_no = $this->request->data['phone_no'];
$lastname = $this->request->data['lastname'];
$master_country_id = $this->request->data['master_country_id'];
$master_province_id = $this->request->data['master_province_id'];
$password = $this->request->data['password'];
$confirm_password = $this->request->data['confirm_password'];
$btn = $this->request->data['btn'];
if(!empty($this->request->data['birthdate'])){
$birthdate = explode("/", $this->request->data['birthdate']);
$birthdate = $birthdate[2].'-'.$birthdate[1].'-'.$birthdate[0];
$this->request->data['birthdate'] = $birthdate;
}else{
$birthdate = '2000-10-10';
}
if(!empty($data)){ $this->request->data['password'] = $hasher->hash($password);
if($this->validateAccount($data)){ $users['created_by'] = 0;
$data['data']['ip'] = $this->request->clientIp();#prr($data); $users['is_used'] = true;
$users['created'] = $dateNow;
$users['dynamic_key'] = 'dynamic_key';
// $users['point'] = 0;
$users['dynamic_key_expiry'] = date('Y-m-d',strtotime('+3 day'));
$users['token'] = Security::hash($this->request->data['username'].date('Y-m-d h:i:s'), 'md5', true);
$users['token_expiry'] = date('Y-m-d',strtotime('+3 day'));
// $users['modified_by'] = $modified_by;
$digits = 4;
$users['pin_code'] = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
if(!empty($this->request->data['accept'])){
$username_check = $this->Users->find('all',[
'conditions' => [
'Users.username' => $this->request->data['username']
]
])->toArray();
$api_core_create_account = Configure::read('Config.apiCore.createAccount'); $email_check = $this->UserPersonals->find('all',[
$http = new Client(); 'conditions' => [
$response = $http->post($api_core_create_account,$data)->body();#prd($response); 'UserPersonals.email' => $this->request->data['email']
$response = json_decode($response,'_full'); ]
//debug($response);exit(); ])->toArray();
if(!empty($response)){
if(trim($response['status']) == 'Success'){ if((empty($username_check))&&(empty($email_check))){
$this->Flash->success(__('create Account completed.')); $users = $this->Users->patchEntity($users, $this->request->getData());
return $this->redirect(['controller' => 'Users', 'action' => 'pinCode/'.$response['result']['data']['token']]); // pr($users);die;
} elseif ($response['result']['message'] == 'Email Duplicate'){
$this->Flash->error(__('Email Duplicate')); //------------------------------ ส่งอีเมล์ -----------------------------------------------------
return $this->redirect(['controller' => 'Users', 'action' => 'signup']); // $data_notification = [];
}else{ // $data_notification['email'] = $this->request->data['email'];
$this->Flash->error(__('create Account Fail')); // $data_notification['pin_code'] = $users['pin_code'];
return $this->redirect(['controller' => 'Users', 'action' => 'signup']); // $this->notification($data_notification);
//----------------------------------------------------------------------------------------------
$this->Users->save($users);
$user_personals['master_country_id'] = $master_country_id;
$user_personals['master_province_id'] = $master_province_id;
$user_personals['user_id'] = $users['id'];
$user_personals['firstname_th'] = $firstname;
$user_personals['lastname_th'] = $lastname;
$user_personals['created_by'] = 1;
$user_personals['created'] = $dateNow;
$user_personals = $this->UserPersonals->patchEntity($user_personals, $this->request->getData());
$this->UserPersonals->save($user_personals);
$this->Flash->success(__('The register success.'));
return $this->redirect(['controller' => 'Users', 'action' => 'pinCode/'.$users['token']]);
return $this->redirect(['action' => 'signin']);
}
$this->Flash->success(__('user or email in program'));
return $this->redirect(['action' => 'signup']);
}else if(empty($this->request->data['accept'])){
$this->Flash->success(__('please accept'));
return $this->redirect(['action' => 'signup']);
}
$this->Flash->error(__('The article could not be saved. Please, try again.'));
} }
} }
}else{ public function notification($data = null)
$this->Flash->error(__('Data Empty')); {
return $this->redirect(['controller' => 'Users', 'action' => 'signin']); if(!empty($data)){
$verify_code = $data['pin_code'];
$from_email = ['support@pakgon.com' => 'Support'];
$to_emails = [$data['email']];
$email = new Email();
$email->transport('gmail');
try {
$email->template('notification_signup', 'connect');
$email->from($from_email);
$email->to($to_emails);
$email->subject('Signup Connect Verify');
$email->emailFormat('html');
$email->viewVars(compact('verify_code'));
#$email->send($message);
$email->send();
} catch (Exception $e) {
echo 'Exception : ', $e->getMessage(), "\n";
} }
#$this->httpStatusCode = 200;
#$this->apiResponse['message'] = 'Signup Connect Completed!!';
} }
die;
} }
#--------------------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------
...@@ -144,10 +279,10 @@ class UsersController extends AppController ...@@ -144,10 +279,10 @@ class UsersController extends AppController
} }
public function notification() // public function notification()
{ // {
$this->viewBuilder()->layout('blank'); // $this->viewBuilder()->layout('blank');
} // }
#Verify Pin Code #Verify Pin Code
public function pinCode($token=null) public function pinCode($token=null)
...@@ -226,19 +361,28 @@ class UsersController extends AppController ...@@ -226,19 +361,28 @@ class UsersController extends AppController
$this->viewBuilder()->layout('blank'); $this->viewBuilder()->layout('blank');
$data = $this->request->data();#debug($data);exit(); $data = $this->request->data();#debug($data);exit();
// pr($data);die;
// pr($this->validateAccount($data));die;
if(!empty($data)){ if(!empty($data)){
// pr($data);die;
if($this->validateAccount($data)){ if($this->validateAccount($data)){
$data['data']['ip'] = $this->request->clientIp();#debug($data);exit(); $data['data']['ip'] = $this->request->clientIp();#debug($data);exit();
// pr($data);die;
$api_core_change_password = Configure::read('Config.apiCore.changePassword');#debug($api_core_change_password);exit(); $api_core_change_password = Configure::read('Config.apiCore.changePassword');#debug($api_core_change_password);exit();
$http = new Client(); $http = new Client();
// pr($http);die;
#$response = json_decode($http->post($api_core_change_password,$data)->body(),'_full'); #$response = json_decode($http->post($api_core_change_password,$data)->body(),'_full');
$response = $http->post($api_core_change_password,$data)->body(); $response = $http->post($api_core_change_password,$data)->body();
// pr($data);die;
#debug($response);exit(); #debug($response);exit();
$response = json_decode($response,'_full'); $response = json_decode($response,'_full');
// pr($response);die;
#debug($response);exit(); #debug($response);exit();
if(!empty($response)){ if(!empty($response)){
// pr($response);die;
#if(trim($response['status']) == 'Success' && !empty($response['result']['type'])){ #if(trim($response['status']) == 'Success' && !empty($response['result']['type'])){
if(trim($response['status']) == 'Success'){ if(trim($response['status']) == 'Success'){
#$this->response->withHeader('Authorization', 'Bearer '.$response['result']['token']); #$this->response->withHeader('Authorization', 'Bearer '.$response['result']['token']);
...@@ -268,4 +412,5 @@ class UsersController extends AppController ...@@ -268,4 +412,5 @@ class UsersController extends AppController
$this->checkToken(); $this->checkToken();
} }
} }
...@@ -71,51 +71,51 @@ class UsersTable extends Table { ...@@ -71,51 +71,51 @@ class UsersTable extends Table {
* @return \Cake\Validation\Validator * @return \Cake\Validation\Validator
*/ */
public function validationDefault(Validator $validator) { public function validationDefault(Validator $validator) {
$validator // $validator
->allowEmpty('id', 'create'); // ->allowEmpty('id', 'create');
$validator // $validator
->scalar('username') // ->scalar('username')
->allowEmpty('username'); // ->allowEmpty('username');
$validator // $validator
->scalar('password') // ->scalar('password')
->allowEmpty('password'); // ->allowEmpty('password');
$validator // $validator
->integer('point') // ->integer('point')
->allowEmpty('point'); // ->allowEmpty('point');
$validator // $validator
->boolean('is_used') // ->boolean('is_used')
->allowEmpty('is_used'); // ->allowEmpty('is_used');
$validator // $validator
->scalar('dynamic_key') // ->scalar('dynamic_key')
->allowEmpty('dynamic_key'); // ->allowEmpty('dynamic_key');
$validator // $validator
->date('dynamic_key_expiry') // ->date('dynamic_key_expiry')
->allowEmpty('dynamic_key_expiry'); // ->allowEmpty('dynamic_key_expiry');
$validator // $validator
->scalar('token') // ->scalar('token')
->allowEmpty('token'); // ->allowEmpty('token');
$validator // $validator
->date('token_expiry') // ->date('token_expiry')
->allowEmpty('token_expiry'); // ->allowEmpty('token_expiry');
$validator // $validator
->requirePresence('created_by', 'create') // ->requirePresence('created_by', 'create')
->notEmpty('created_by'); // ->notEmpty('created_by');
$validator // $validator
->allowEmpty('modified_by'); // ->allowEmpty('modified_by');
$validator // $validator
->scalar('pin_code') // ->scalar('pin_code')
->allowEmpty('pin_code'); // ->allowEmpty('pin_code');
return $validator; return $validator;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment