PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/module/Login/src/Login/Controller/AuthController.php

https://gitlab.com/mnomansheikh/ampuz
PHP | 393 lines | 329 code | 34 blank | 30 comment | 12 complexity | 1feb4e6efef8de965cf06428c5dc7dcb MD5 | raw file
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Noman Sheikh
  5. * Date: 8/6/2015
  6. * Time: 12:53 PM
  7. */
  8. //module/Login/src/Controller/AuthController.php
  9. namespace Login\Controller;
  10. use Zend\Console\Adapter\AdapterInterface;
  11. use Zend\Mvc\Controller\AbstractActionController;
  12. use Zend\Form\Annotation\AnnotationBuilder;
  13. use Zend\View\Model\ViewModel;
  14. use Login\Model\User;
  15. //use Login\Model\MyAuthStorage;
  16. use Zend\Session\Container;
  17. use Login\Model\CurrentUser;
  18. use Login\Model\UserProfile;
  19. use Login\Form\UserProfileForm;
  20. class AuthController extends AbstractActionController
  21. {
  22. protected $form;
  23. protected $userSession;
  24. protected $storage;
  25. protected $authservice;
  26. // protected $currentUserId;
  27. protected $CurrentUser;
  28. protected $UserProfileTable;
  29. // public function getUserProfileTable()
  30. // {
  31. // if (!$this->UserProfileTable) {
  32. // $sm = $this->getServiceLocator();
  33. // $this->UserProfileTable = $sm->get('Login\Model\UserProfileTable');
  34. // }
  35. // return $this->UserProfileTable;
  36. // }
  37. public function getCurrentUserTable()
  38. {
  39. if (!$this->CurrentUser) {
  40. $sm = $this->getServiceLocator();
  41. $this->CurrentUser = $sm->get('Login\Model\CurrentUser');
  42. }
  43. return $this->CurrentUser;
  44. }
  45. public function getAuthService()
  46. {
  47. if (!$this->authservice) {
  48. $this->authservice = $this->getServiceLocator()
  49. ->get('AuthService');
  50. }
  51. return $this->authservice;
  52. }
  53. public function getCurrentuserTableGateway()
  54. {
  55. if (!$this->authservice) {
  56. $this->authservice = $this->getServiceLocator()
  57. ->get('CurrentuserTableGateway');
  58. }
  59. return $this->authservice;
  60. }
  61. public function getSessionStorage()
  62. {
  63. if (!$this->storage) {
  64. $this->storage = $this->getServiceLocator()
  65. ->get('Login\Model\MyAuthStorage');
  66. }
  67. return $this->storage;
  68. }
  69. public function getForm()
  70. {
  71. if (!$this->form) {
  72. $user = new User();
  73. $builder = new AnnotationBuilder();
  74. $this->form = $builder->createForm($user);
  75. }
  76. return $this->form;
  77. }
  78. public function loginAction()
  79. {
  80. //if already login, redirect to success page
  81. if ($this->getAuthService()->hasIdentity()) {
  82. return $this->redirect()->toRoute('application');
  83. }
  84. $form = $this->getForm();
  85. return array(
  86. 'form' => $form,
  87. 'messages' => $this->flashmessenger()->getMessages()
  88. );
  89. }
  90. public function authenticateAction()
  91. {
  92. $form = $this->getForm();
  93. $redirect = 'login';
  94. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  95. $request = $this->getRequest();
  96. if ($request->isPost()) {
  97. $form->setData($request->getPost());
  98. if ($form->isValid()) {
  99. //check authentication...
  100. $this->getAuthService()->getAdapter()
  101. ->setIdentity($request->getPost('username'))
  102. ->setCredential($request->getPost('password'));
  103. $result = $this->getAuthService()->authenticate();
  104. foreach ($result->getMessages() as $message) {
  105. $code= $result->getCode();
  106. switch ($code) {
  107. case $result::FAILURE_IDENTITY_NOT_FOUND:
  108. /** do stuff for nonexistent identity **/
  109. $message= "Invalid Username Or Password";
  110. break;
  111. case $result::FAILURE_CREDENTIAL_INVALID:
  112. /** do stuff for invalid credential **/
  113. $message= "Your Account has been disabled please contact your administrator";
  114. break;
  115. case $result::SUCCESS:
  116. /** do stuff for successful authentication **/
  117. $message= "Login Successfully";
  118. break;
  119. default:
  120. /** do stuff for other failure **/
  121. $message= "Invalid Username Or Password";
  122. break;
  123. }
  124. //save message temporary into flashmessenger
  125. $this->flashmessenger()->addMessage($message);
  126. }
  127. if ($result->isValid()) {
  128. $redirect = 'application';
  129. // Store variables in session
  130. $userSession = new Container('user');
  131. $userSession->username = $request->getPost('username');
  132. $userSession->password = $request->getPost('password');
  133. //current login user data
  134. $results = $this->getCurrentUserTable()->fetchAll($dbAdapter);
  135. // foreach ($results as $result) {
  136. $user_id = $results['user_id'];
  137. $user_type = $results['user_role_id'];
  138. $user_companies = $results['company_id'];
  139. $user_regions = $results['region_id'];
  140. $user_countries = $results['country_id'];
  141. $user_indicators = $results['indicator_id'];
  142. $userSession->user_id = $user_id;
  143. $userSession->user_type = $user_type;
  144. $userSession->user_companies = $user_companies;
  145. $userSession->user_regions = $user_regions;
  146. $userSession->user_countries = $user_countries;
  147. $userSession->user_indicators = $user_indicators;
  148. // }
  149. //check if it has rememberMe :
  150. if ($request->getPost('rememberme') == 1) {
  151. $this->getSessionStorage()
  152. ->setRememberMe(1);
  153. //set storage again
  154. $this->getAuthService()->setStorage($this->getSessionStorage());
  155. }
  156. $this->getAuthService()->getStorage()->write($request->getPost('username'));
  157. }
  158. }
  159. }
  160. return $this->redirect()->toRoute($redirect);
  161. }
  162. public function logoutAction()
  163. {
  164. $this->getSessionStorage()->forgetMe();
  165. $this->getAuthService()->clearIdentity();
  166. $userSession = new Container('user');
  167. unset($_SESSION['user']);
  168. $this->flashmessenger()->addMessage("You've been logged out");
  169. return $this->redirect()->toRoute('login');
  170. }
  171. /*public function addAction()
  172. {
  173. if ($this->getAuthService()->hasIdentity()) {
  174. $userprofile = new UserProfile();
  175. $builder = new AnnotationBuilder();
  176. $form = $builder->createForm($userprofile);
  177. $request = $this->getRequest();
  178. if ($request->isPost()) {
  179. $form->bind($userprofile);
  180. $form->setData($request->getPost());
  181. if ($form->isValid()) {
  182. print_r($form->getData());
  183. }
  184. }
  185. return array('form' => $form);
  186. }
  187. else{
  188. return $this->redirect()->toRoute('login');
  189. }
  190. }*/
  191. /*user profile*/
  192. public function ListAction()
  193. {
  194. //Check user is logged in
  195. if ($this->getAuthService()->hasIdentity()) {
  196. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  197. $form = new UserProfileForm($dbAdapter);
  198. $userSession = new Container('user');
  199. // echo 'Logged in as ' . $userSession->user_type;
  200. //For admin user
  201. // grab the paginator from the CommunityTable
  202. $paginator = $this->getCurrentUserTable()->fetchAll_user($dbAdapter, true);
  203. // set the current page to what has been passed in query string, or to 1 if none set
  204. $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
  205. // set the number of items per page to 10
  206. $paginator->setItemCountPerPage(10);
  207. $view = new ViewModel(array(
  208. 'paginator' => $paginator,
  209. 'form' => $form
  210. ));
  211. $view->setTemplate('login/auth/list');
  212. return $view;
  213. } /* if ($userSession->user_type == _CHAMPION_) {
  214. $form = new EnergyForm();
  215. $form->get('submit')->setValue('Add');
  216. $request = $this->getRequest();
  217. if ($request->isPost()) {
  218. $energy = new Energy();
  219. $form->setInputFilter($energy->getInputFilter());
  220. $form->setData($request->getPost());
  221. if ($form->isValid()) {
  222. $energy->exchangeArray($form->getData());
  223. $this->getEnergyTable()->saveEnergy($energy, null);
  224. // Redirect to list of albums
  225. return $this->redirect()->toRoute('energy');
  226. }
  227. }
  228. return array('form' => $form, 'energy' => $this->getEnergyTable()->fetchAll($dbAdapter));
  229. }
  230. */
  231. else {
  232. return $this->redirect()->toRoute('login');
  233. }
  234. // return new ViewModel(array(
  235. // 'energy' => $this->getEnergyTable()->fetchAll(),
  236. //
  237. // ));
  238. // $form->setVariable('form', $form);
  239. }
  240. public function addAction()
  241. {
  242. //Check user is logged in
  243. if ($this->getAuthService()->hasIdentity()) {
  244. $userSession = new Container('user');
  245. // echo 'Logged in as ' . $userSession->user_type;
  246. //For admin user
  247. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  248. if ($userSession->user_type == _ADMIN_) {
  249. $form = new UserProfileForm($dbAdapter);
  250. $form->get('submit')->setValue('Save');
  251. $form->get('cancel')->setValue('Cancel');
  252. $request = $this->getRequest();
  253. if ($request->isPost()) {
  254. $user_profile = new UserProfile();
  255. $user_profile->setDbAdapter($dbAdapter);
  256. $form->setInputFilter($user_profile->getInputFilter());
  257. $data = array_merge_recursive(
  258. $this->getRequest()->getPost()->toArray(),
  259. $this->getRequest()->getFiles()->toArray()
  260. );
  261. $form->setData($request->getPost());
  262. $form->setData($data);
  263. if ($form->isValid()) {
  264. $user_profile->exchangeArray($form->getData());
  265. $this->getCurrentUserTable()->saveUserProfile($user_profile, $dbAdapter);
  266. // Redirect to list
  267. return $this->redirect()->toRoute('login', array(
  268. 'action' => 'list'
  269. ));
  270. }
  271. }
  272. //return array('form' => $form, 'event' => $this->getUserProfileTable()->fetchAll($dbAdapter));
  273. $view = new ViewModel(array(
  274. 'form' => $form
  275. ));
  276. $view->setTemplate('login/auth/add');
  277. return $view;
  278. }
  279. if ($userSession->user_type == _CHAMPION_) {
  280. return $this->redirect()->toRoute('application');
  281. }
  282. } else {
  283. return $this->redirect()->toRoute('login');
  284. }
  285. }
  286. public function editAction()
  287. {
  288. //Check user is logged in
  289. if ($this->getAuthService()->hasIdentity()) {
  290. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  291. $id = (int)$this->params()->fromRoute('id', 0);
  292. try {
  293. $UserProfileTable = $this->getCurrentUserTable()->getUserProfile($id, $dbAdapter);
  294. } catch (\Exception $ex) {
  295. return $this->redirect()->toRoute('login', array(
  296. 'action' => 'index'
  297. ));
  298. }
  299. $form = new UserProfileForm($dbAdapter);
  300. $UserProfile = new UserProfile();
  301. $form->bind($UserProfileTable);
  302. //for session varibles
  303. $form->get('submit')->setValue('Save');
  304. $form->get('cancel')->setValue('Cancel');
  305. $request = $this->getRequest();
  306. if ($request->isPost()) {
  307. $UserProfile->setDbAdapter($dbAdapter);
  308. $form->setInputFilter($UserProfile->getInputFilter());
  309. $form->setData($request->getPost());
  310. if ($id > 0) {
  311. $form->getInputFilter()->get('user_name')->setRequired(false);
  312. $form->getInputFilter()->get('user_password')->setRequired(false);
  313. $form->getInputFilter()->get('confirm_password')->setRequired(false);
  314. }
  315. if ($form->isValid()) {
  316. $UserProfile->exchangeArray($form->getData());
  317. $this->getCurrentUserTable()->saveUserProfile($UserProfile, $dbAdapter);
  318. // Redirect to list
  319. return $this->redirect()->toRoute('login', array(
  320. 'action' => 'list'
  321. ));
  322. }
  323. }
  324. // $user = $this->getCurrentUserTable()->fetchAll_event($dbAdapter,$id);
  325. return array(
  326. 'id' => $id,
  327. 'form' => $form,
  328. // 'user' => $user,
  329. );
  330. } else {
  331. return $this->redirect()->toRoute('login');
  332. }
  333. }
  334. /*user profile*/
  335. }