Airship\Cabin\Bridge\Blueprint\UserAccounts::editUser PHP Method

editUser() public method

Edit a user's account information
public editUser ( integer $userId, array $post = [] ) : boolean
$userId integer
$post array
return boolean
    public function editUser(int $userId, array $post = []) : bool
    {
        $this->db->beginTransaction();
        $updates = [];
        foreach (\array_keys($post['groups']) as $i) {
            $post['groups'][$i] += 0;
        }
        $oldGroups = $this->getUsersGroups($userId);
        $delete = \array_diff($oldGroups, $post['groups']);
        $insert = \array_diff($post['groups'], $oldGroups);
        // Manage group changes:
        foreach ($insert as $ins) {
            $this->db->insert('airship_users_groups', ['userid' => $userId, 'groupid' => $ins]);
        }
        foreach ($delete as $del) {
            $this->db->delete('airship_users_groups', ['userid' => $userId, 'groupid' => $del]);
        }
        foreach (['username', 'uniqueid', 'email', 'display_name', 'real_name'] as $f) {
            $updates[$f] = $post[$f] ?? null;
        }
        if (!empty($post['password'])) {
            $updates['password'] = $this->airship_auth->createHash(new HiddenString($post['password']));
        }
        $updates['custom_fields'] = \json_encode(\json_decode($post['custom_fields'], true));
        $this->db->update('airship_users', $updates, ['userid' => $userId]);
        return $this->db->commit();
    }

Usage Example

Example #1
0
 /**
  * Edit a user's information
  *
  * @route crew/users/edit/{id}
  * @param string $userId
  */
 public function editUser(string $userId = '')
 {
     $userId = (int) $userId;
     $user = $this->account->getUserAccount($userId, true);
     $post = $this->post(new EditUserFilter());
     if ($post) {
         if ($this->account->editUser($userId, $post)) {
             \Airship\redirect($this->airship_cabin_prefix . '/crew/users');
         }
     }
     $this->lens('crew/user_edit', ['active_link' => 'bridge-link-admin-crew-users', 'user' => $user, 'groups' => $this->account->getGroupTree()]);
 }