src/User/Service/User.php
public function save(array $data, $triggerEvents = true) 
    {
		// All the services we need
		$main = $this->getServiceLocator()->get('neobazaar.service.main');
		$userRepository = $main->getUserEntityRepository();
		$em = $main->getEntityManager();
		$userId = isset($data['id']) ? $data['id'] : null;
		$email = isset($data['email']) ? $data['email'] : null;
		$user = null;
		
		// try to get the user using email address
		if(null === $userId && null !== $email) {
			$user = $userRepository->findOneBy(array('email' => $email));
		}
		
		// If userId is not provied it is a registration
		// this not mean that the user is not already in the database!
		$form = null === $userId ? 
			$this->getServiceLocator()->get('user.form.register') : 
			$this->getServiceLocator()->get('user.form.useradd'); // @todo change alias into user.form.account

		if(isset($data['dateBorn'])) {
			if(false === $data['dateBorn']) {
				unset($data['dateBorn']);
			} else {
				$data['dateBorn'] = $this->dateToSqlFormat($data['dateBorn']);
			}
		}
		
		// @todo: get from a service
		if(null === $user) {
			if(null === $userId) {
				$user = new UserEntity();
			} else {
				$user = $this->getEntity($userId);
			}
		}
		
		if(!$this->checkIfOwnerOrAdmin($user) && null !== $userId) {
			throw new \Exception('Non possiedi i permessi per agire su questo documento');
		}
		
		// IMPORTANT: If admin skip the current password validation
		if(method_exists($form, 'setSkipCurrentPasswordValidation')) {
			$form->setSkipCurrentPasswordValidation($this->checkIfAdmin($user));
		}
		
		$form->bind($user);
		$form->setData($data);
		
		if (!$form->isValid()) {
			throw new \Exception(serialize($form->getMessages()));
		}
		
		// If we have it here is valid. Getting it from the form will return the current crypted password
		$password = isset($data['password']) ? trim($data['password']) : null;
		if(null !== $password) {
			$password = 60 == strlen($password) ? $password : $this->getCryptedPassword($password);  
			$user->setPassword($password);
		}
		$url = $this->getServiceLocator()->get('ControllerPluginManager')->get('Url');
		
		// We o not have hash id or mail in this event
		$event = new UserRegistrationEvent(__FUNCTION__, null, array(
		    'siteurl' => $url->fromRoute('home', array(), array('force_canonical' => true)),
		));
		
		if($triggerEvents) {
			$this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this, $event);
		}
		
		$em->persist($user);
		$em->flush();

		$userModel = $this->getServiceLocator()->get('user.model.user');
		$userModel->init($user, $this->getServiceLocator());
		$event = new UserRegistrationEvent(__FUNCTION__, null, array(
		    'email' => $userModel->email,
		    'siteurl' => $url->fromRoute('home', array(), array('force_canonical' => true)),
		    'hash' => $userModel->hashId
		));
		
		if($triggerEvents) {
			$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, $event);
		}
		
		return $user;
    }
public function save(array $data, $triggerEvents = true) 
    {
		// All the services we need
		$main = $this->getServiceLocator()->get('neobazaar.service.main');
		$userRepository = $main->getUserEntityRepository();
		$em = $main->getEntityManager();
		$userId = isset($data['id']) ? $data['id'] : null;
		$email = isset($data['email']) ? $data['email'] : null;
		$user = null;
		
		// try to get the user using email address
		if(null === $userId && null !== $email) {
			$user = $userRepository->findOneBy(array('email' => $email));
		}
		
		// If userId is not provied it is a registration
		// this not mean that the user is not already in the database!
		$form = null === $userId ? 
			$this->getServiceLocator()->get('user.form.register') : 
			$this->getServiceLocator()->get('user.form.useradd'); // @todo change alias into user.form.account

		if(isset($data['dateBorn'])) {
			if(false === $data['dateBorn']) {
				unset($data['dateBorn']);
			} else {
				$data['dateBorn'] = $this->dateToSqlFormat($data['dateBorn']);
			}
		}
		
		// @todo: get from a service
		if(null === $user) {
			if(null === $userId) {
				$user = new UserEntity();
			} else {
				$user = $this->getEntity($userId);
			}
		}
		
		if(!$this->checkIfOwnerOrAdmin($user) && null !== $userId) {
			throw new \Exception('Non possiedi i permessi per agire su questo documento');
		}
		
		// IMPORTANT: If admin skip the current password validation
		if(method_exists($form, 'setSkipCurrentPasswordValidation')) {
			$form->setSkipCurrentPasswordValidation($this->checkIfAdmin($user));
		}
		
		$form->bind($user);
		$form->setData($data);
		
		if (!$form->isValid()) {
			throw new \Exception(serialize($form->getMessages()));
		}
		
		// If we have it here is valid. Getting it from the form will return the current crypted password
		$password = isset($data['password']) ? trim($data['password']) : null;
		if(null !== $password) {
			$password = 60 == strlen($password) ? $password : $this->getCryptedPassword($password);  
			$user->setPassword($password);
		}
		$url = $this->getServiceLocator()->get('ControllerPluginManager')->get('Url');
		
		// We o not have hash id or mail in this event
		$event = new UserRegistrationEvent(__FUNCTION__, null, array(
		    'siteurl' => $url->fromRoute('home', array(), array('force_canonical' => true)),
		));
		
		if($triggerEvents) {
			$this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this, $event);
		}
		
		$em->persist($user);
		$em->flush();

		$userModel = $this->getServiceLocator()->get('user.model.user');
		$userModel->init($user, $this->getServiceLocator());
		$event = new UserRegistrationEvent(__FUNCTION__, null, array(
		    'email' => $userModel->email,
		    'siteurl' => $url->fromRoute('home', array(), array('force_canonical' => true)),
		    'hash' => $userModel->hashId
		));
		
		if($triggerEvents) {
			$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, $event);
		}
		
		return $user;
    }
Create or update an user

@param array $data
@param boolean $triggerEvents Whether or not to trigger events, needed when silently add user
@throws \Exception
@return Neobazaar\Entity\User