Example #1
0
 /**
  * Update user profile
  * 
  * @param int 
  * @access public
  * @return void
  */
 public function action_update($id = false)
 {
     if (!is_numeric($id)) {
         \Response::redirect('admin/settings/user/list');
     }
     // Cast user id param to int
     $id = (int) $id;
     // Redirect if user don't exists
     if (!\Sentry::user_exists($id)) {
         \Response::redirect('admin/settings/user/list');
     }
     $user = \Sentry::user($id);
     $user_group = new \Sentry_User((int) $id);
     $user_group1 = $user_group->groups();
     $user_group2 = current($user_group1);
     $user_data = array('id' => $user->get('id'), 'first_name' => $user->get('metadata.first_name'), 'last_name' => $user->get('metadata.last_name'), 'email' => $user->get('email'), 'username' => $user->get('username'), 'user_group' => $user_group2['name']);
     if (\Input::post()) {
         // Validate input parameters
         $val = \Validation::forge('admin_details_validation');
         $val->add('first_name', 'First Name')->add_rule('required')->add_rule('min_length', 2)->add_rule('max_length', 255);
         $val->add('last_name', 'Last Name')->add_rule('required')->add_rule('min_length', 2)->add_rule('max_length', 255);
         $val->add('email', 'Email')->add_rule('required')->add_rule('valid_email');
         $val->add('password', 'Password')->add_rule('min_length', 8);
         $val->add('confirm_password', 'Confirm Password')->add_rule('required_with', 'password')->add_rule('match_field', 'password');
         $val->add('username', 'Username')->add_rule('required')->add_rule('unique', array('users', 'username', $id));
         if ($val->run()) {
             // Get Input parameters
             $post_data = \Input::post();
             try {
                 $fields = array('username' => $post_data['username'], 'email' => $post_data['email'], 'password' => $post_data['password'], 'user_group' => $post_data['user_group'], 'metadata' => array('first_name' => $post_data['first_name'], 'last_name' => $post_data['last_name']));
                 if (empty($post_data['password'])) {
                     unset($fields['password']);
                 }
                 $item = new \Sentry_User((int) $id);
                 $update = $item->update($fields);
                 // $item->remove_from_group((int)$fields['user_group']);
                 // $item->add_to_group((int)$fields['user_group']);
                 if ($update) {
                     $user_groups = $item->groups();
                     if (!empty($user_groups)) {
                         // Remove user from all other groups...
                         foreach ($user_groups as $value) {
                             $item->remove_from_group((int) $value['id']);
                         }
                     }
                     $item = new \Sentry_User((int) $id);
                     // ...and add it to selected one
                     $item->add_to_group((int) $fields['user_group']);
                     \Messages::success('User Details Successfully updated.');
                     \Response::redirect(\Uri::admin('current'));
                 } else {
                     \Messages::error('There was an error while trying to update User details.');
                 }
             } catch (Sentry\SentryException $e) {
                 \Messages::error($e->get_message());
             }
         } else {
             if ($val->error() != array()) {
                 // Show validation errors
                 \Messages::error('<strong>There was an error while trying to update User details</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     \View::set_global('title', 'Update User Details');
     \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('user_data', $user_data);
 }
Example #2
0
 public function action_update($id = false)
 {
     if (!is_numeric($id)) {
         \Response::redirect('admin/user/list');
     }
     // Get user to edit
     if (!\Sentry::user_exists((int) $id)) {
         \Response::redirect('admin/user/list');
     }
     \View::set_global('title', 'Edit User');
     // Get groups
     $groups = \Sentry::group()->all('front');
     // Update group details
     if (\Input::post('details', false)) {
         $item = new \Sentry_User((int) $id);
         $val = \User\Controller_Admin_Validate::forge('update', $item['id']);
         if ($val->run()) {
             // Get POST values
             $insert = \Input::post();
             array_walk($insert, create_function('&$val', '$val = trim($val);'));
             try {
                 // Generate random username
                 //$username 	= '******' . \Str::random('numeric', 16);
                 $username = $insert['email'];
                 $email = $insert['email'];
                 $password = $insert['password'];
                 $user_group = $insert['user_group'];
                 $activated = $insert['activated'];
                 $email_client = $insert['email_client'];
                 $insert['guest'] = $user_group == 3 ? 1 : 0;
                 unset($insert['email'], $insert['password'], $insert['confirm_password'], $insert['user_group'], $insert['details'], $insert['save'], $insert['exit'], $insert['activated'], $insert['email_client']);
                 $only_billing = array('business_name', 'purchase_limit_value', 'purchase_limit_period', 'master', 'note', 'credit_account', 'guest');
                 // Set shipping data to be same as billing by default
                 /*foreach($insert as $key => $value)
                   {
                       if(!in_array($key, $only_billing) && strpos($key, 'shipping_') === false)
                       {
                           if(empty($insert['shipping_'.$key]))
                               $insert['shipping_'.$key] = $value;
                       }
                   }*/
                 // create the user - no activation required
                 $vars = array('username' => $username, 'email' => $email, 'password' => $password, 'metadata' => $insert, 'activated' => $activated);
                 // Send email to user with new password
                 if ($email_client == 1 && !empty($vars['password'])) {
                     $email_data = array('site_title' => \Config::get('site_title'), 'customer_identity' => ucwords($item->get('metadata.first_name') . ' ' . $item->get('metadata.last_name')), 'new_password' => $vars['password']);
                     $this->autoresponder($item, $email_data);
                 }
                 if (empty($vars['password'])) {
                     unset($vars['password']);
                 }
                 if ($item->update($vars)) {
                     //Change user group if needed
                     $user_groups = $item->groups();
                     if (!empty($user_groups)) {
                         // Remove user from all other groups...
                         foreach ($user_groups as $value) {
                             $item->remove_from_group((int) $value['id']);
                         }
                     }
                     $item = new \Sentry_User((int) $id);
                     // ...and add it to selected one
                     $item->add_to_group((int) $user_group);
                     \Messages::success('User successfully updated.');
                     \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/user/list/') : \Uri::admin('current'));
                 } else {
                     // show validation errors
                     \Messages::error('<strong>' . 'There was an error while trying to update user' . '</strong>');
                 }
             } catch (\Sentry\SentryException $e) {
                 // show validation errors
                 \Messages::error('<strong>' . 'There was an error while trying to update user' . '</strong>');
                 $errors = $e->getMessage();
                 \Messages::error($errors);
             }
         } else {
             if ($val->error() != array()) {
                 // show validation errors
                 \Messages::error('<strong>' . 'There was an error while trying to update user' . '</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     $user = new \Sentry_User((int) $id);
     // Get single user group
     $user_group = $user->groups();
     $user_group = current($user_group);
     $user->group = $user_group;
     \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('user', $user)->set('groups', $groups);
 }