securityContext = $securityContext; $this->em = $em; $this->ii = $ii; $this->helper = $helper; } /** * @param LoginHistory $item * @return bool */ public function update(LoginHistory $item){ if(empty($item)) return false; return $this->quickUpdate($item); } /** * @param LoginHistory $item * @return bool */ public function quickUpdate(LoginHistory $item){ if(empty($item)) return false; try { if(!$this->em->contains($item)) $this->em->persist($item); $this->em->flush(); } catch (OptimisticLockException $e) { //die('Exception : '.$e->getMessage()); return false; } catch (ORMException $e) { return false; } return true; } /** * @param $type * @param CustomUserInterface|null $user * @return bool */ public function addEntry($type, CustomUserInterface $user = null){ if(!is_int($type)) return false; //if user isnt provided we get the current user if(is_null($user)){ $user = $this->securityContext->getToken()->getUser(); } //if there is nt any actual user //end here if(is_null($user)) return false; if(!$user->isLogMe()) return false; $device_name = $this->helper->getMachineName(false,false); if(!$device_name) $device_name = "Unknown Device"; $entry = new LoginHistory(); $entry->setUserId($user->getId()); $entry->setType($type); $entry->setDeviceName($device_name); $entry->setDatetime(new \DateTime()); $entry->setHost(gethostname()); $ipInfo = $this->ii->getInfo(); $entry->setIpInfos($ipInfo['geolocalisation']); $entry->setIp($this->ii->getIp()); $entry->setRequest($this->getRequest()); return $this->quickUpdate($entry); } /** * @param CustomUserInterface $user * @param string $format * @return LoginHistory[]|array|bool */ public function getEntryForUser(CustomUserInterface $user, $format = "object"){ $id = $user->getId(); if(isset($id) && !empty($id)){ $ret = $this->em->getRepository('UserBasisBundle:LoginHistory')->findBy(array('user_id' => $id)); if($format == "array"){ $ret2 = array(); foreach($ret as $key=>$log){ /** * @var LoginHistory $log */ $item = array( 'datetime' => $log->getDatetime()->format('d/m/Y H:i'), 'host' => $log->getHost(), 'ip' => $log->getIp(), 'ipInfo' => $log->getIpInfos(), 'device' => $log->getDeviceName(), 'type' => $log->getType() ); if($item['device'] == "NaN") $item['device'] = "Unknown Device"; $ret2[] = $item; } $ret = array_reverse($ret2); } return $ret; } return false; } /** * @return array */ protected function getRequest(){ $output = array(); $output['request_uri'] = $_SERVER['REQUEST_URI']; return $output; } }