예제 #1
0
 /**
  * Save the BotPassword to the database
  * @param string $operation 'update' or 'insert'
  * @param Password|null $password Password to set.
  * @return bool Success
  */
 public function save($operation, Password $password = null)
 {
     $conds = array('bp_user' => $this->centralId, 'bp_app_id' => $this->appId);
     $fields = array('bp_token' => MWCryptRand::generateHex(User::TOKEN_LENGTH), 'bp_restrictions' => $this->restrictions->toJson(), 'bp_grants' => FormatJson::encode($this->grants));
     if ($password !== null) {
         $fields['bp_password'] = $password->toString();
     } elseif ($operation === 'insert') {
         $fields['bp_password'] = PasswordFactory::newInvalidPassword()->toString();
     }
     $dbw = self::getDB(DB_MASTER);
     switch ($operation) {
         case 'insert':
             $dbw->insert('bot_passwords', $fields + $conds, __METHOD__, array('IGNORE'));
             break;
         case 'update':
             $dbw->update('bot_passwords', $fields, $conds, __METHOD__);
             break;
         default:
             return false;
     }
     $ok = (bool) $dbw->affectedRows();
     if ($ok) {
         $this->token = $dbw->selectField('bot_passwords', 'bp_token', $conds, __METHOD__);
         $this->isSaved = true;
     }
     return $ok;
 }