Commit 5e93d833 by Teeradone-PIM

teeradone: register

parent 2905751c
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Core\Configure;
use Cake\Http\Client;
use Cake\Routing\Router;
use Cake\I18n\Time;
use Cake\Mailer\Email;
use Cake\Utility\Security;
class UsersController extends AppController
{
......@@ -86,46 +89,178 @@ class UsersController extends AppController
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');
}
// 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
public function createAccount()
{
$this->viewBuilder()->layout('blank');
$data = $this->request->data();
$this->loadModel('UserPersonals');
$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)){
if($this->validateAccount($data)){
$data['data']['ip'] = $this->request->clientIp();#prr($data);
$this->request->data['password'] = $hasher->hash($password);
$users['created_by'] = 0;
$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');
$http = new Client();
$response = $http->post($api_core_create_account,$data)->body();#prd($response);
$response = json_decode($response,'_full');
//debug($response);exit();
if(!empty($response)){
if(trim($response['status']) == 'Success'){
$this->Flash->success(__('create Account completed.'));
return $this->redirect(['controller' => 'Users', 'action' => 'pinCode/'.$response['result']['data']['token']]);
} elseif ($response['result']['message'] == 'Email Duplicate'){
$this->Flash->error(__('Email Duplicate'));
return $this->redirect(['controller' => 'Users', 'action' => 'signup']);
}else{
$this->Flash->error(__('create Account Fail'));
return $this->redirect(['controller' => 'Users', 'action' => 'signup']);
$email_check = $this->UserPersonals->find('all',[
'conditions' => [
'UserPersonals.email' => $this->request->data['email']
]
])->toArray();
if((empty($username_check))&&(empty($email_check))){
$users = $this->Users->patchEntity($users, $this->request->getData());
// pr($users);die;
//------------------------------ ส่งอีเมล์ -----------------------------------------------------
// $data_notification = [];
// $data_notification['email'] = $this->request->data['email'];
// $data_notification['pin_code'] = $users['pin_code'];
// $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{
$this->Flash->error(__('Data Empty'));
return $this->redirect(['controller' => 'Users', 'action' => 'signin']);
public function notification($data = null)
{
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
}
public function notification()
{
$this->viewBuilder()->layout('blank');
}
// public function notification()
// {
// $this->viewBuilder()->layout('blank');
// }
#Verify Pin Code
public function pinCode($token=null)
......@@ -226,19 +361,28 @@ class UsersController extends AppController
$this->viewBuilder()->layout('blank');
$data = $this->request->data();#debug($data);exit();
// pr($data);die;
// pr($this->validateAccount($data));die;
if(!empty($data)){
// pr($data);die;
if($this->validateAccount($data)){
$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();
$http = new Client();
// pr($http);die;
#$response = json_decode($http->post($api_core_change_password,$data)->body(),'_full');
$response = $http->post($api_core_change_password,$data)->body();
// pr($data);die;
#debug($response);exit();
$response = json_decode($response,'_full');
// pr($response);die;
#debug($response);exit();
if(!empty($response)){
// pr($response);die;
#if(trim($response['status']) == 'Success' && !empty($response['result']['type'])){
if(trim($response['status']) == 'Success'){
#$this->response->withHeader('Authorization', 'Bearer '.$response['result']['token']);
......@@ -268,4 +412,5 @@ class UsersController extends AppController
$this->checkToken();
}
}
......@@ -71,51 +71,51 @@ class UsersTable extends Table {
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator) {
$validator
->allowEmpty('id', 'create');
// $validator
// ->allowEmpty('id', 'create');
$validator
->scalar('username')
->allowEmpty('username');
// $validator
// ->scalar('username')
// ->allowEmpty('username');
$validator
->scalar('password')
->allowEmpty('password');
// $validator
// ->scalar('password')
// ->allowEmpty('password');
$validator
->integer('point')
->allowEmpty('point');
// $validator
// ->integer('point')
// ->allowEmpty('point');
$validator
->boolean('is_used')
->allowEmpty('is_used');
// $validator
// ->boolean('is_used')
// ->allowEmpty('is_used');
$validator
->scalar('dynamic_key')
->allowEmpty('dynamic_key');
// $validator
// ->scalar('dynamic_key')
// ->allowEmpty('dynamic_key');
$validator
->date('dynamic_key_expiry')
->allowEmpty('dynamic_key_expiry');
// $validator
// ->date('dynamic_key_expiry')
// ->allowEmpty('dynamic_key_expiry');
$validator
->scalar('token')
->allowEmpty('token');
// $validator
// ->scalar('token')
// ->allowEmpty('token');
$validator
->date('token_expiry')
->allowEmpty('token_expiry');
// $validator
// ->date('token_expiry')
// ->allowEmpty('token_expiry');
$validator
->requirePresence('created_by', 'create')
->notEmpty('created_by');
// $validator
// ->requirePresence('created_by', 'create')
// ->notEmpty('created_by');
$validator
->allowEmpty('modified_by');
// $validator
// ->allowEmpty('modified_by');
$validator
->scalar('pin_code')
->allowEmpty('pin_code');
// $validator
// ->scalar('pin_code')
// ->allowEmpty('pin_code');
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