Пример #1
0
 /**
  * Signup if not logged in
  *
  * @return void
  */
 public function indexAction()
 {
     $this->layout('layout/signup');
     $viewData = array();
     $signupForm = new SignupForm();
     $signupForm->setAttribute('action', $this->url()->fromRoute('users-signup'));
     $request = $this->getRequest();
     if ($request->isPost()) {
         $data = $request->getPost()->toArray();
         $signupForm->setInputFilter(User::getInputFilter());
         $signupForm->setData($data);
         if ($signupForm->isValid()) {
             $files = $request->getFiles()->toArray();
             $data = $signupForm->getData();
             $data['avatar'] = $files['avatar']['name'] != '' ? $files['avatar']['name'] : null;
             if ($data['avatar'] !== null) {
                 $size = new Size(array('max' => 2048000));
                 $isImage = new IsImage();
                 $filename = $data['avatar'];
                 $adapter = new \Zend\File\Transfer\Adapter\Http();
                 $adapter->setValidators(array($size, $isImage), $filename);
                 if (!$adapter->isValid($filename)) {
                     $errors = array();
                     foreach ($adapter->getMessages() as $key => $row) {
                         $errors[] = $row;
                     }
                     $signupForm->setMessages(array('avatar' => $errors));
                 }
                 $destPath = 'data/tmp/';
                 $adapter->setDestination($destPath);
                 $fileinfo = $adapter->getFileInfo();
                 preg_match('/.+\\/(.+)/', $fileinfo['avatar']['type'], $matches);
                 $newFilename = sprintf('%s.%s', sha1(uniqid(time(), true)), $matches[1]);
                 $adapter->addFilter('File\\Rename', array('target' => $destPath . $newFilename, 'overwrite' => true));
                 if ($adapter->receive($filename)) {
                     $data['avatar'] = base64_encode(file_get_contents($destPath . $newFilename));
                     if (file_exists($destPath . $newFilename)) {
                         unlink($destPath . $newFilename);
                     }
                 }
             }
             unset($data['repeat_password']);
             unset($data['csrf']);
             unset($data['register']);
             $response = ApiClient::registerUser($data);
             if ($response['result'] == true) {
                 $this->flashMessenger()->addMessage('Account created!');
                 return $this->redirect()->toRoute('wall', array('username' => $data['username']));
             }
         }
     }
     $viewData['signupForm'] = $signupForm;
     return $viewData;
 }
Пример #2
0
 /**
  * Create new user
  * 
  * 
  * @access public
  * @uses User
  * @uses UserForm
  * 
  * @return ViewModel
  */
 public function newAction()
 {
     $variables = array();
     $query = $this->getServiceLocator()->get('wrapperQuery')->setEntity('Users\\Entity\\User');
     $countriesService = $this->getServiceLocator()->get('losi18n-countries');
     $languagesService = $this->getServiceLocator()->get('losi18n-languages');
     $userModel = $this->getServiceLocator()->get('Users\\Model\\User');
     $userObj = new User();
     $options = array();
     $options['query'] = $query;
     $locale = "en";
     $options['countries'] = $countriesService->getAllCountries($locale);
     $options['languages'] = $languagesService->getAllLanguages($locale);
     $options['excludedRoles'] = array(Role::USER_ROLE);
     $auth = new AuthenticationService();
     $storage = $auth->getIdentity();
     if (!$auth->hasIdentity() || $auth->hasIdentity() && !in_array(Role::ADMIN_ROLE, $storage['roles'])) {
         $options['excludedRoles'][] = Role::ADMIN_ROLE;
     }
     $isAdminUser = false;
     if ($auth->hasIdentity() && in_array(Role::ADMIN_ROLE, $storage['roles'])) {
         $isAdminUser = true;
     }
     $form = new UserForm(null, $options);
     $request = $this->getRequest();
     if ($request->isPost()) {
         // Make certain to merge the files info!
         $fileData = $request->getFiles()->toArray();
         $data = array_merge_recursive($request->getPost()->toArray(), $fileData);
         $query->setEntity('Users\\Entity\\User');
         $form->setInputFilter($userObj->getInputFilter($query));
         $form->setData($data);
         $isCustomValidationValid = true;
         if ($data['email'] != $data['confirmEmail']) {
             $form->get('confirmEmail')->setMessages(array("email doesnt match"));
             $isCustomValidationValid = false;
         }
         if ($data['password'] != $data['confirmPassword']) {
             $form->get('confirmPassword')->setMessages(array("password doesnt match"));
             $isCustomValidationValid = false;
         }
         if ($form->isValid() && $isCustomValidationValid === true) {
             $userModel->saveUser($data, null, $isAdminUser);
             if ($isAdminUser) {
                 $url = $this->getEvent()->getRouter()->assemble(array('action' => 'index'), array('name' => 'users'));
                 $this->redirect()->toUrl($url);
             } else {
                 $variables['success'] = true;
             }
         }
     }
     $variables['userForm'] = $this->getFormView($form);
     $statement = new Statement();
     $variables['statements'] = $statement->statements;
     return new ViewModel($variables);
 }