Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller\Admin;
- use App\Controller\AppController;
- use Cake\Auth\DefaultPasswordHasher;
- use Cake\Event\Event;
- use Cake\I18n\Time;
- use Cake\Mailer\Email;
- use Cake\Routing\Router;
- /**
- * Admins Controller
- *
- * @property \App\Model\Table\AdminsTable $Admins
- *
- */
- class AdminsController extends AppController
- {
- public function beforeFilter(Event $event)
- {
- parent::beforeFilter($event); // TODO: Change the autogenerated stub
- $this->Auth->allow(['forgotPassword', 'resetPassword', 'generateToken']);
- }
- public function login()
- {
- $a = new DefaultPasswordHasher();
- $this->viewBuilder()->layout('admin_blank');
- if ($this->request->is('post')) {
- $user = $this->Auth->identify();
- if ($user) {
- if ($user['status'] != 'A') {
- $session = $this->request->session();
- $session->destroy();
- $this->Flash->error('Account is blocked! Contact Admin');
- $this->redirect($this->Auth->logout());
- } else {
- $admin = $this->Admins->get($user['id']);
- $dataToSave['last_login'] = new Time();
- $admin = $this->Admins->patchEntity($admin, $dataToSave);
- $this->Admins->save($admin);
- $this->Auth->setUser($user);
- $this->redirect($this->Auth->redirectUrl());
- }
- } else {
- $this->Flash->error('Username or password is incorrect');
- }
- }
- }
- public function logout()
- {
- $session = $this->request->session();
- $session->destroy();
- return $this->redirect($this->Auth->logout());
- }
- public function dashboard()
- {
- $id = $this->Auth->user('id');
- $admin = $this->Admins->get($id);
- $breadcrumb = array(
- array(
- 'name' => 'Sub Admins',
- 'link' => Router::url(
- array('controller' => 'Admins', 'action' => 'index')
- )
- ),
- array(
- 'name' => $admin->full_name,
- 'link' => ''
- )
- );
- $this->set(compact('admin', 'breadcrumb'));
- }
- public function settings()
- {
- if ($this->request->is(['post', 'put'])) {
- $data = $this->request->data;
- $admin = $this->Admins->get($this->Auth->user('id'));
- $hasher = new DefaultPasswordHasher();
- $oldPassword = $data['old_password'];
- if ($hasher->check($oldPassword, $admin->password)) {
- if ($data['new_password'] == $data['confirm_password']) {
- if (strlen($data['new_password']) >= 6) {
- $dataToSave['password'] = $hasher->hash($data['new_password']);
- $admin = $this->Admins->patchEntity($admin, $dataToSave);
- if ($this->Admins->save($admin)) {
- $this->Flash->success(__('Successfully updated.'));
- return $this->redirect(['controller' => 'Admins', 'action' => 'index']);
- } else {
- if ($admin->errors()) {
- $this->Flash->error(__($this->Utils->validationError($admin->errors())));
- } else {
- $this->Flash->error(__('Error occurred! Try again'));
- }
- }
- } else {
- $this->Flash->error(_('Password must be greater then 6 characters'));
- }
- } else {
- $this->Flash->error(__('Password mismatch'));
- }
- } else {
- $this->Flash->error(__('Old Password is incorrect'));
- }
- }
- $this->loadModel('Constants');
- $terms = $this->Constants->find('all', [
- 'conditions' => ['field' => 'APP_TERMS_AND_CONDITION']
- ])->first();
- $aboutUs = $this->Constants->find('all', [
- 'conditions' => ['field' => 'APP_ABOUT_US']
- ])->first();
- $disclaimer = $this->Constants->find('all', [
- 'conditions' => ['field' => 'DISCLAIMER']
- ])->first();
- $analystBackground = $this->Constants->find('all', [
- 'conditions' => ['field' => 'ANALYST_BACKGROUND_IMAGE']
- ])->first();
- $howItWorks = $this->Constants->find('all', [
- 'conditions' => ['field' => 'HOW_IT_WORKS']
- ])->first();
- $breadcrumb = array(
- array(
- 'name' => 'Settings',
- 'link' => ''
- ),
- );
- $this->set(compact('breadcrumb', 'terms', 'aboutUs', 'analystBackground', 'disclaimer', 'howItWorks'));
- }
- public function forgotPassword()
- {
- if ($this->request->is(['post', 'put'])) {
- $data = $this->request->data();
- $emailId = $data['email'];
- if ($this->Admins->exists(['email' => $emailId])) {
- $admin = $this->Admins->find('all', [
- 'conditions' => ['Admins.email' => $emailId]
- ])->first();
- $code = rand(10000, 100000);
- $dataToSave['reset_code'] = $code;
- $resetUrl = Router::url(
- array(
- 'controller' => 'Admins',
- 'action' => 'resetPassword',
- $this->Utils->encrypt($emailId),
- $this->Utils->encrypt($code)
- ),
- true
- );
- if ($this->Admins->updateAll($dataToSave, ['email' => $emailId])) {
- $email = new Email();
- $email->template('forgot_password', 'default')
- ->viewVars(['name' => $admin->full_name, 'link' => $resetUrl])
- ->emailFormat('html')
- ->to($emailId)
- ->subject('Forgot Password [BigProfit]')
- ->send();
- $this->Flash->success(_('Reset password send to your email'));
- } else {
- $this->Flash->error(_('Error Occurred! Try again'));
- }
- } else {
- $this->Flash->error(_('Email not registered yet! Contact Admin'));
- }
- }
- return $this->redirect(['controller' => 'Admins', 'action' => 'login']);
- }
- public function resetPassword($email = null, $resetCode = null)
- {
- $email = $this->Utils->decrypt($email);
- $resetCode = $this->Utils->decrypt($resetCode);
- if ($resetCode == '') {
- $this->Flash->error(_('Request URL expired! Try again'));
- return $this->redirect(['controller' => 'Admins', 'action' => 'login']);
- } else if (!$this->Admins->exists(['email' => $email, 'reset_code' => $resetCode])) {
- $this->Flash->error(_('Request URL expired! Try again'));
- return $this->redirect(['controller' => 'Admins', 'action' => 'login']);
- } else if ($this->request->is(['post', 'put'])) {
- $data = $this->request->data();
- if ($data['password'] == $data['confirm_password']) {
- if (strlen($data['password']) >= 6) {
- $hasher = new DefaultPasswordHasher();
- $dataToSave['password'] = $hasher->hash($data['password']);
- $dataToSave['reset_code'] = '';
- if ($this->Admins->updateAll($dataToSave, ['email' => $email])) {
- $this->Flash->success(_('Password changed'));
- return $this->redirect(['controller' => 'Admins', 'action' => 'login']);
- } else {
- $this->Flash->error(_('Error Occurred! Try again'));
- }
- } else {
- $this->Flash->error(_('Password must be greater then 6 characters'));
- }
- } else {
- $this->Flash->error(_('Password mismatch'));
- }
- }
- $this->request->data = [];
- $this->viewBuilder()->layout('blank');
- }
- public function changeProfilePic()
- {
- if ($this->request->is(['post']) && isset($this->request->data['pic'])) {
- $file = $this->request->data['pic'];
- if ($this->Utils->validateImage($file)) {
- $path = UPLOADS . "admins/{$this->Auth->user('id')}";
- if (!is_dir($path)) {
- mkdir($path);
- }
- $this->Utils->uploadImageFile($file, "{$path}/profile_pic.png");
- $this->Flash->success('Profile picture changed successfully');
- } else {
- $this->Flash->error('Error Occurred! Try again');
- }
- }
- $this->redirect(['action' => 'settings']);
- }
- }
Add Comment
Please, Sign In to add comment