Guest User

Untitled

a guest
Mar 28th, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.18 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller\Admin;
  4.  
  5. use App\Controller\AppController;
  6. use Cake\Auth\DefaultPasswordHasher;
  7. use Cake\Event\Event;
  8. use Cake\I18n\Time;
  9. use Cake\Mailer\Email;
  10. use Cake\Routing\Router;
  11.  
  12. /**
  13.  * Admins Controller
  14.  *
  15.  * @property \App\Model\Table\AdminsTable $Admins
  16.  *
  17.  */
  18. class AdminsController extends AppController
  19. {
  20.  
  21.     public function beforeFilter(Event $event)
  22.     {
  23.         parent::beforeFilter($event); // TODO: Change the autogenerated stub
  24.         $this->Auth->allow(['forgotPassword', 'resetPassword', 'generateToken']);
  25.  
  26.     }
  27.  
  28.     public function login()
  29.     {
  30.         $a = new DefaultPasswordHasher();
  31.  
  32.         $this->viewBuilder()->layout('admin_blank');
  33.         if ($this->request->is('post')) {
  34.  
  35.             $user = $this->Auth->identify();
  36.             if ($user) {
  37.                 if ($user['status'] != 'A') {
  38.                     $session = $this->request->session();
  39.                     $session->destroy();
  40.                     $this->Flash->error('Account is blocked! Contact Admin');
  41.                     $this->redirect($this->Auth->logout());
  42.                 } else {
  43.                     $admin = $this->Admins->get($user['id']);
  44.                     $dataToSave['last_login'] = new Time();
  45.                     $admin = $this->Admins->patchEntity($admin, $dataToSave);
  46.                     $this->Admins->save($admin);
  47.                     $this->Auth->setUser($user);
  48.                     $this->redirect($this->Auth->redirectUrl());
  49.                 }
  50.             } else {
  51.                 $this->Flash->error('Username or password is incorrect');
  52.             }
  53.         }
  54.     }
  55.  
  56.     public function logout()
  57.     {
  58.         $session = $this->request->session();
  59.         $session->destroy();
  60.         return $this->redirect($this->Auth->logout());
  61.     }
  62.  
  63.     public function dashboard()
  64.     {
  65.         $id = $this->Auth->user('id');
  66.         $admin = $this->Admins->get($id);
  67.  
  68.         $breadcrumb = array(
  69.             array(
  70.                 'name' => 'Sub Admins',
  71.                 'link' => Router::url(
  72.                     array('controller' => 'Admins', 'action' => 'index')
  73.                 )
  74.             ),
  75.             array(
  76.                 'name' => $admin->full_name,
  77.                 'link' => ''
  78.             )
  79.         );
  80.  
  81.         $this->set(compact('admin', 'breadcrumb'));
  82.     }
  83.  
  84.     public function settings()
  85.     {
  86.  
  87.         if ($this->request->is(['post', 'put'])) {
  88.             $data = $this->request->data;
  89.             $admin = $this->Admins->get($this->Auth->user('id'));
  90.  
  91.             $hasher = new DefaultPasswordHasher();
  92.             $oldPassword = $data['old_password'];
  93.             if ($hasher->check($oldPassword, $admin->password)) {
  94.                 if ($data['new_password'] == $data['confirm_password']) {
  95.                     if (strlen($data['new_password']) >= 6) {
  96.                         $dataToSave['password'] = $hasher->hash($data['new_password']);
  97.                         $admin = $this->Admins->patchEntity($admin, $dataToSave);
  98.                         if ($this->Admins->save($admin)) {
  99.                             $this->Flash->success(__('Successfully updated.'));
  100.                             return $this->redirect(['controller' => 'Admins', 'action' => 'index']);
  101.                         } else {
  102.                             if ($admin->errors()) {
  103.                                 $this->Flash->error(__($this->Utils->validationError($admin->errors())));
  104.                             } else {
  105.                                 $this->Flash->error(__('Error occurred! Try again'));
  106.                             }
  107.  
  108.                         }
  109.                     } else {
  110.                         $this->Flash->error(_('Password must be greater then 6 characters'));
  111.                     }
  112.  
  113.                 } else {
  114.                     $this->Flash->error(__('Password mismatch'));
  115.                 }
  116.             } else {
  117.                 $this->Flash->error(__('Old Password is incorrect'));
  118.             }
  119.         }
  120.  
  121.         $this->loadModel('Constants');
  122.         $terms = $this->Constants->find('all', [
  123.             'conditions' => ['field' => 'APP_TERMS_AND_CONDITION']
  124.         ])->first();
  125.  
  126.         $aboutUs = $this->Constants->find('all', [
  127.             'conditions' => ['field' => 'APP_ABOUT_US']
  128.         ])->first();
  129.  
  130.         $disclaimer = $this->Constants->find('all', [
  131.             'conditions' => ['field' => 'DISCLAIMER']
  132.         ])->first();
  133.  
  134.         $analystBackground = $this->Constants->find('all', [
  135.             'conditions' => ['field' => 'ANALYST_BACKGROUND_IMAGE']
  136.         ])->first();
  137.         $howItWorks = $this->Constants->find('all', [
  138.             'conditions' => ['field' => 'HOW_IT_WORKS']
  139.         ])->first();
  140.  
  141.  
  142.         $breadcrumb = array(
  143.             array(
  144.                 'name' => 'Settings',
  145.                 'link' => ''
  146.             ),
  147.         );
  148.         $this->set(compact('breadcrumb', 'terms', 'aboutUs', 'analystBackground', 'disclaimer', 'howItWorks'));
  149.     }
  150.  
  151.     public function forgotPassword()
  152.     {
  153.         if ($this->request->is(['post', 'put'])) {
  154.  
  155.             $data = $this->request->data();
  156.             $emailId = $data['email'];
  157.             if ($this->Admins->exists(['email' => $emailId])) {
  158.                 $admin = $this->Admins->find('all', [
  159.                     'conditions' => ['Admins.email' => $emailId]
  160.                 ])->first();
  161.                 $code = rand(10000, 100000);
  162.  
  163.                 $dataToSave['reset_code'] = $code;
  164.                 $resetUrl = Router::url(
  165.                     array(
  166.                         'controller' => 'Admins',
  167.                         'action' => 'resetPassword',
  168.                         $this->Utils->encrypt($emailId),
  169.                         $this->Utils->encrypt($code)
  170.                     ),
  171.                     true
  172.                 );
  173.  
  174.                 if ($this->Admins->updateAll($dataToSave, ['email' => $emailId])) {
  175.                     $email = new Email();
  176.                     $email->template('forgot_password', 'default')
  177.                         ->viewVars(['name' => $admin->full_name, 'link' => $resetUrl])
  178.                         ->emailFormat('html')
  179.                         ->to($emailId)
  180.                         ->subject('Forgot Password [BigProfit]')
  181.                         ->send();
  182.  
  183.                     $this->Flash->success(_('Reset password send to your email'));
  184.                 } else {
  185.                     $this->Flash->error(_('Error Occurred! Try again'));
  186.                 }
  187.  
  188.  
  189.             } else {
  190.                 $this->Flash->error(_('Email not registered yet! Contact Admin'));
  191.             }
  192.         }
  193.         return $this->redirect(['controller' => 'Admins', 'action' => 'login']);
  194.     }
  195.  
  196.     public function resetPassword($email = null, $resetCode = null)
  197.     {
  198.  
  199.         $email = $this->Utils->decrypt($email);
  200.         $resetCode = $this->Utils->decrypt($resetCode);
  201.         if ($resetCode == '') {
  202.             $this->Flash->error(_('Request URL expired! Try again'));
  203.             return $this->redirect(['controller' => 'Admins', 'action' => 'login']);
  204.         } else if (!$this->Admins->exists(['email' => $email, 'reset_code' => $resetCode])) {
  205.             $this->Flash->error(_('Request URL expired! Try again'));
  206.             return $this->redirect(['controller' => 'Admins', 'action' => 'login']);
  207.         } else if ($this->request->is(['post', 'put'])) {
  208.  
  209.             $data = $this->request->data();
  210.  
  211.             if ($data['password'] == $data['confirm_password']) {
  212.                 if (strlen($data['password']) >= 6) {
  213.                     $hasher = new DefaultPasswordHasher();
  214.                     $dataToSave['password'] = $hasher->hash($data['password']);
  215.                     $dataToSave['reset_code'] = '';
  216.                     if ($this->Admins->updateAll($dataToSave, ['email' => $email])) {
  217.                         $this->Flash->success(_('Password changed'));
  218.                         return $this->redirect(['controller' => 'Admins', 'action' => 'login']);
  219.                     } else {
  220.                         $this->Flash->error(_('Error Occurred! Try again'));
  221.                     }
  222.                 } else {
  223.                     $this->Flash->error(_('Password must be greater then 6 characters'));
  224.                 }
  225.  
  226.             } else {
  227.                 $this->Flash->error(_('Password mismatch'));
  228.             }
  229.  
  230.         }
  231.         $this->request->data = [];
  232.         $this->viewBuilder()->layout('blank');
  233.  
  234.     }
  235.  
  236.     public function changeProfilePic()
  237.     {
  238.  
  239.         if ($this->request->is(['post']) && isset($this->request->data['pic'])) {
  240.  
  241.             $file = $this->request->data['pic'];
  242.  
  243.             if ($this->Utils->validateImage($file)) {
  244.                 $path = UPLOADS . "admins/{$this->Auth->user('id')}";
  245.  
  246.                 if (!is_dir($path)) {
  247.                     mkdir($path);
  248.                 }
  249.                 $this->Utils->uploadImageFile($file, "{$path}/profile_pic.png");
  250.                 $this->Flash->success('Profile picture changed successfully');
  251.             } else {
  252.                 $this->Flash->error('Error Occurred! Try again');
  253.             }
  254.  
  255.         }
  256.  
  257.         $this->redirect(['action' => 'settings']);
  258.     }
  259.  
  260. }
Add Comment
Please, Sign In to add comment