コード例 #1
0
 /**
  * Save the user data to the database
  *
  * When creating a new user, set all the attributes for the user (except user_id) and call save. Save will
  * set the user_id for the user.
  *
  */
 public function save($check_unique_email = true)
 {
     Logger::log("Enter: function User::save");
     // global var $_base_url has been removed - please, use PA::$url static variable
     $sql = '';
     try {
         if (!$this->login_name || !$this->password || !$this->first_name || !$this->email) {
             Logger::log("Throwing exception REQUIRED_PARAMETERS_MISSING | Message: Required parameters missing", LOGGER_ERROR);
             throw new PAException(REQUIRED_PARAMETERS_MISSING, "Required parameters missing");
         }
         if (!$this->is_active) {
             Logger::log("Throwing exception SAVING_DELETED_USER | Message: Saving a deleted user is not allowed", LOGGER_ERROR);
             throw new PAException(SAVING_DELETED_USER, "Saving a deleted user is not allowed");
         }
         // added to remove unnecessary check whether the word begins or ends with a 'space' character
         $this->first_name = @trim($this->first_name);
         $this->last_name = @trim($this->last_name);
         $this->login_name = @trim($this->login_name);
         $this->password = @trim($this->password);
         $this->email = @trim($this->email);
         // checking the user data When creating a new user or updating the existing user value
         $this->check_authenticated_user_data();
         if ($this->is_new) {
             // Make sure that the login name is unique.
             $sql = 'SELECT * FROM {users} WHERE login_name = ? AND is_active <> ? AND is_active <> ?';
             $data = array($this->login_name, DELETED, UNVERIFIED);
             $res = Dal::query($sql, $data);
             if ($res->numRows() > 0) {
                 Logger::log(" Throwing exception USER_LOGINNAME_TAKEN | Message: This Login name has already been taken", LOGGER_ERROR);
                 throw new PAException(USER_LOGINNAME_TAKEN, "This Login name has already been taken");
             }
             if ($check_unique_email) {
                 // make sure that the email address is unique
                 $sql = 'SELECT * FROM {users} WHERE email = ? AND is_active <> ?';
                 $data = array($this->email, DELETED);
                 $res = Dal::query($sql, $data);
                 if ($res->numRows() > 0) {
                     Logger::log(" Throwing exception USER_EMAIL_NOT_UNIQUE | Message: Email address must be unique", LOGGER_ERROR);
                     throw new PAException(USER_EMAIL_NOT_UNIQUE, "Email address that you have given is already taken please give another email address");
                 }
             }
             $this->user_id = Dal::next_id("User");
             if ($this->api_call != true) {
                 // only encrypt the password if this is not an API call
                 $this->password = md5($this->password);
             }
             if (!isset($this->created)) {
                 $this->created = time();
             }
             $this->changed = $this->created;
             $this->last_login = time();
             if ($this->api_call == true) {
                 $sql = 'INSERT into {users} (user_id, core_id, login_name, password, first_name, last_name, email, is_active, created, changed, picture, picture_width, picture_height, avatar, avatar_width, avatar_height, avatar_small, avatar_small_width, avatar_small_height, last_login) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? , ?, ?, ?, ?, ?)';
                 $data = array($this->user_id, $this->core_id, $this->login_name, $this->password, $this->first_name, $this->last_name, $this->email, $this->is_active, $this->created, $this->changed, $this->picture, $this->picture_dimensions['width'], $this->picture_dimensions['height'], $this->avatar, $this->avatar_dimensions['width'], $this->avatar_dimensions['height'], $this->avatar_small, $this->avatar_small_dimensions['width'], $this->avatar_small_dimensions['height'], $this->last_login);
             } else {
                 $sql = 'INSERT into {users} (user_id, login_name, password, first_name, last_name, email, is_active, created, changed, picture, last_login) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
                 $data = array($this->user_id, $this->login_name, $this->password, $this->first_name, $this->last_name, $this->email, $this->is_active, $this->created, $this->changed, $this->picture, $this->last_login);
             }
             Dal::query($sql, $data);
             // Code for sending the data to ping server: begin
             $PingClient = new PingClient();
             global $host;
             // defined in config.inc
             // global var $path_prefix has been removed - please, use PA::$path static variable
             $pa_url = $host;
             $pa_activity = PA_ACTIVITY_USER_ADDED;
             $pa_user_url = PA::$url . PA_ROUTE_USER_PUBLIC . '/' . $this->user_id;
             $pa_user_name = $this->first_name . ' ' . $this->last_name;
             $param_array = array('pa_url' => $pa_url, 'pa_activity' => $pa_activity, 'pa_user_url' => $pa_user_url, 'pa_user_name' => $pa_user_name);
             $PingClient->set_params($param_array);
             // @$PingClient->send_ping();
             // Code for sending the data to ping server: end
             // By default first user is being assigned as ADMIN (admin role id is 2).
             if ($this->user_id == SUPER_USER_ID) {
                 $user_roles = array();
                 $user_roles[0] = array('role_id' => ADMINISTRATOR_ROLE, 'extra' => serialize(array('user' => false, 'network' => true, 'groups' => array())));
                 $this->set_user_role($user_roles);
             }
         } else {
             if ($check_unique_email) {
                 // make sure that the email address is unique
                 $sql = 'SELECT * FROM {users} WHERE email = ?';
                 $data = array($this->email);
                 $res = Dal::query($sql, $data);
                 if ($res->numRows() > 0) {
                     $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
                     if ($row->user_id != $this->user_id) {
                         Logger::log(" Throwing exception USER_EMAIL_NOT_UNIQUE | Message: Email address must be unique", LOGGER_ERROR);
                         throw new PAException(USER_EMAIL_NOT_UNIQUE, "Email address that you have given is already taken please give another email address");
                     }
                 }
             }
             if ($this->api_call == true) {
                 $sql = 'UPDATE {users} SET login_name = ?, password = ?, first_name = ?, last_name = ?, email = ?, is_active = ?, changed = ?, picture = ?, picture_width = ?, picture_height = ?, avatar = ?, avatar_width = ?, avatar_height = ?, avatar_small = ?, avatar_small_width = ?, avatar_small_height = ? WHERE user_id = ?';
                 $data = array($this->login_name, $this->password, $this->first_name, $this->last_name, $this->email, 1, time(), $this->picture, $this->picture_dimensions['width'], $this->picture_dimensions['height'], $this->avatar, $this->avatar_dimensions['width'], $this->avatar_dimensions['height'], $this->avatar_small, $this->avatar_small_dimensions['width'], $this->avatar_small_dimensions['height'], $this->user_id);
             } else {
                 $sql = 'UPDATE {users} SET login_name = ?, password = ?, first_name = ?, last_name = ?, email = ?, is_active = ?, changed = ?, picture = ? WHERE user_id = ?';
                 $data = array($this->login_name, $this->password, $this->first_name, $this->last_name, $this->email, 1, time(), $this->picture, $this->user_id);
             }
             Dal::query($sql, $data);
         }
         // all done - commit to database
         Dal::commit();
     } catch (PAException $e) {
         Dal::rollback();
         throw $e;
     }
     // save the core user data so that search can find it
     $data = array();
     $data['first_name'] = $this->first_name;
     $data['last_name'] = $this->last_name;
     $data['email'] = $this->email;
     $data['login_name'] = $this->login_name;
     $old_data = User::load_user_profile($this->user_id, $this->user_id, BASIC, null);
     // ensure we are NOT duplicating data here!!
     foreach ($old_data as $i => $d) {
         $k = $d['name'];
         $v = $d['value'];
         if (empty($data[$k])) {
             // only ever preserve if we are NOT submiting this field
             $data[$k] = $v;
         }
     }
     // turn it all to a format that this function undersatbds
     $user_data = array();
     foreach ($data as $k => $v) {
         $user_data[] = array('name' => $k, 'value' => $v, 'uid' => $this->user_id, 'perm' => 1, 'type' => BASIC);
     }
     $this->save_user_profile($user_data, BASIC);
     $this->is_new = FALSE;
     if ($this->tags) {
         // Attach an array of string tags to the user
         //Tag::add_tags_to_user($this->user_id, $this->tags);
     }
     Logger::log("Exit: function User::save");
 }
コード例 #2
0
ファイル: User.php プロジェクト: CivicCommons/oldBellCaPA
 /**
  * Save the user data to the database
  *
  * When creating a new user, set all the attributes for the user (except user_id) and call save. Save will
  * set the user_id for the user.
  *
  */
 public function save()
 {
     Logger::log("Enter: function User::save");
     global $base_url;
     $sql = '';
     try {
         if (!$this->login_name || !$this->password || !$this->first_name || !$this->email) {
             Logger::log("User::save Throwing exception REQUIRED_PARAMETERS_MISSING | Message: Required parameters missing", LOGGER_ERROR);
             throw new PAException(REQUIRED_PARAMETERS_MISSING, "Required parameters missing: login_name:{$this->login_name}, password:{$this->password}, first_name:{$this->first_name}, email:{$this->email}");
         }
         if (!$this->is_active) {
             Logger::log("Throwing exception SAVING_DELETED_USER | Message: Saving a deleted user is not allowed", LOGGER_ERROR);
             throw new PAException(SAVING_DELETED_USER, "Saving a deleted user is not allowed");
         }
         // checking the user data When creating a new user or updating the existing user value
         $this->check_authenticated_user_data();
         if ($this->is_new) {
             // Make sure that the login name is unique.
             $sql = 'SELECT * FROM {users} WHERE login_name = ? AND is_active <> ? AND is_active <> ?';
             $data = array($this->login_name, DELETED, UNVERIFIED);
             $res = Dal::query($sql, $data);
             if ($res->numRows() > 0) {
                 Logger::log(" Throwing exception USER_LOGINNAME_TAKEN | Message: This Login name has already been taken", LOGGER_ERROR);
                 throw new PAException(USER_LOGINNAME_TAKEN, "This Login name has already been taken");
             }
             // make sure that the email address is unique
             $sql = 'SELECT * FROM {users} WHERE email = ? AND is_active <> ?';
             $data = array($this->email, DELETED);
             $res = Dal::query($sql, $data);
             if ($res->numRows() > 0) {
                 Logger::log(" Throwing exception USER_EMAIL_NOT_UNIQUE | Message: Email address must be unique", LOGGER_ERROR);
                 throw new PAException(USER_EMAIL_NOT_UNIQUE, "Email address that you have given is already taken please give another email address");
             }
             $this->user_id = Dal::next_id("User");
             $this->password = md5($this->password);
             $this->created = time();
             $this->changed = $this->created;
             $this->last_login = $this->created;
             $sql = 'INSERT into {users} (user_id, login_name, password, first_name, last_name, email, is_active, created, changed, picture, last_login) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
             $data = array($this->user_id, $this->login_name, $this->password, $this->first_name, $this->last_name, $this->email, $this->is_active, $this->created, $this->changed, $this->picture, $this->last_login);
             Dal::query($sql, $data);
             // Code for sending the data to ping server: begin
             $PingClient = new PingClient();
             global $host;
             // defined in config.inc
             global $path_prefix;
             $pa_url = $host;
             $pa_activity = PA_ACTIVITY_USER_ADDED;
             $pa_user_url = $base_url . '/user.php?uid=' . $this->user_id;
             $pa_user_name = $this->first_name . ' ' . $this->last_name;
             $param_array = array('pa_url' => $pa_url, 'pa_activity' => $pa_activity, 'pa_user_url' => $pa_user_url, 'pa_user_name' => $pa_user_name);
             $PingClient->set_params($param_array);
             // @$PingClient->send_ping();
             // Code for sending the data to ping server: end
             // By default first user is being assigned as ADMIN (admin role id is 2).
             if ($this->user_id == 1) {
                 $user_role = array(2);
                 $this->set_user_role($user_role);
             }
         } else {
             // make sure that the email address is unique
             $sql = 'SELECT * FROM {users} WHERE email = ?';
             $data = array($this->email);
             $res = Dal::query($sql, $data);
             if ($res->numRows() > 0) {
                 $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
                 if ($row->user_id != $this->user_id) {
                     Logger::log(" Throwing exception USER_EMAIL_NOT_UNIQUE | Message: Email address must be unique", LOGGER_ERROR);
                     throw new PAException(USER_EMAIL_NOT_UNIQUE, "Email address that you have given is already taken please give another email address");
                 }
             }
             $sql = 'UPDATE {users} SET login_name = ?, password = ?, first_name = ?, last_name = ?, email = ?, is_active = ?, changed = ?, picture = ? WHERE user_id = ?';
             $data = array($this->login_name, $this->password, $this->first_name, $this->last_name, $this->email, 1, time(), $this->picture, $this->user_id);
             Dal::query($sql, $data);
         }
         // all done - commit to database
         Dal::commit();
     } catch (PAException $e) {
         Dal::rollback();
         throw $e;
     }
     $this->is_new = FALSE;
     if ($this->tags) {
         // Attach an array of string tags to the user
         //Tag::add_tags_to_user($this->user_id, $this->tags);
     }
     Logger::log("Exit: function User::save");
 }