Harmonize JSON API error messages
This commit is contained in:
+27
-21
@@ -99,14 +99,14 @@ class User extends PhpObject {
|
||||
|
||||
private function addUser($sEmail, $sLang, $sTimezone, $sNickName='') {
|
||||
$bSuccess = false;
|
||||
$sDesc = '';
|
||||
$sLangId = '';
|
||||
|
||||
$iUserId = $this->oDb->insertRow(
|
||||
self::USER_TABLE,
|
||||
array('email'=>$sEmail, 'language'=>$sLang, 'timezone'=>$sTimezone)
|
||||
);
|
||||
|
||||
if($iUserId == 0) $sDesc = 'lang:error.commit_db';
|
||||
if($iUserId == 0) $sLangId = 'error.commit_db';
|
||||
else $bSuccess = true;
|
||||
|
||||
//Extra optional values
|
||||
@@ -115,16 +115,16 @@ class User extends PhpObject {
|
||||
$this->updateGravatar($iUserId, $sEmail);
|
||||
}
|
||||
|
||||
return Livetrail::getResult($bSuccess, $sDesc, [Db::getId(self::USER_TABLE) => $iUserId]);
|
||||
return Livetrail::getResult($bSuccess, $sLangId, [Db::getId(self::USER_TABLE) => $iUserId]);
|
||||
}
|
||||
|
||||
public function setSubscription($bSubscribed) {
|
||||
if($this->getUserId() > 0) {
|
||||
$iSubscribed = $bSubscribed?1:0;
|
||||
$iUserId = $this->oDb->updateRow(self::USER_TABLE, $this->getUserId(), array('subscribed'=>$iSubscribed));
|
||||
if(!$iUserId) return Livetrail::getResult(false, 'lang:error.commit_db');
|
||||
if(!$iUserId) return Livetrail::getResult(false, 'error.commit_db');
|
||||
$this->asUserInfo['subscribed'] = $iSubscribed;
|
||||
return Livetrail::getResult(true, $iSubscribed?'lang:account.subscribed':'lang:account.unsubscribed');
|
||||
return Livetrail::getResult(true, $iSubscribed?'account.subscribed':'account.unsubscribed');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,12 +140,13 @@ class User extends PhpObject {
|
||||
|
||||
public function login($sEmail, $sPassword, $sLang, $sTimezone, $sNickName='') {
|
||||
$bSuccess = false;
|
||||
$sDesc = '';
|
||||
$bSubscribe = false;
|
||||
$sLangId = '';
|
||||
$sEmail = strtolower(trim($sEmail));
|
||||
|
||||
//Check email value
|
||||
if(!filter_var($sEmail, FILTER_VALIDATE_EMAIL)) {
|
||||
$sDesc = 'lang:account.invalid_email';
|
||||
$sLangId = 'account.invalid_email';
|
||||
}
|
||||
else {
|
||||
//Check Email presence in DB
|
||||
@@ -161,13 +162,13 @@ class User extends PhpObject {
|
||||
//Is Admin
|
||||
if($asDBUser['clearance'] >= self::CLEARANCE_ADMIN) {
|
||||
//Request a password
|
||||
if($sPassword === '') $sDesc = empty($asDBUser['password'])?'lang:account.set_password':'lang:account.password_required';
|
||||
if($sPassword === '') $sLangId = empty($asDBUser['password'])?'account.set_password':'account.password_required';
|
||||
|
||||
//Set password
|
||||
elseif(empty($asDBUser['password'])) {
|
||||
if(!$this->oDb->updateRow(self::USER_TABLE, $iUserId, array('password' => password_hash($sPassword, PASSWORD_DEFAULT)))) $sDesc = 'lang:error.commit_db';
|
||||
if(!$this->oDb->updateRow(self::USER_TABLE, $iUserId, array('password' => password_hash($sPassword, PASSWORD_DEFAULT)))) $sLangId = 'error.commit_db';
|
||||
else {
|
||||
$sDesc = 'lang:account.password_set';
|
||||
$sLangId = 'account.password_set';
|
||||
$bSuccess = true;
|
||||
}
|
||||
}
|
||||
@@ -175,20 +176,21 @@ class User extends PhpObject {
|
||||
//Check password
|
||||
elseif(password_verify($sPassword, $asDBUser['password'])) {
|
||||
$bSuccess = true;
|
||||
$sDesc = 'lang:account.logged_in';
|
||||
$sLangId = 'account.logged_in';
|
||||
}
|
||||
else $sDesc = 'lang:account.invalid_credentials';
|
||||
else $sLangId = 'account.invalid_credentials';
|
||||
}
|
||||
else {
|
||||
$bSuccess = true;
|
||||
$sDesc = 'lang:account.logged_in';
|
||||
$sLangId = 'account.logged_in';
|
||||
}
|
||||
}
|
||||
else {
|
||||
//Unknown user, create it
|
||||
$asAddResult = $this->addUser($sEmail, $sLang, $sTimezone, $sNickName);
|
||||
$bSuccess = $asAddResult['result'];
|
||||
$sDesc = $bSuccess?'subscribe_user':$asAddResult['desc'];
|
||||
$bSubscribe = $bSuccess;
|
||||
$sLangId = $bSuccess?'':$asAddResult['desc_lang_id'];
|
||||
$iUserId = $asAddResult['data'][Db::getId(self::USER_TABLE)] ?? 0;
|
||||
}
|
||||
}
|
||||
@@ -199,7 +201,7 @@ class User extends PhpObject {
|
||||
$this->setTokenCookie();
|
||||
}
|
||||
|
||||
return Livetrail::getResult($bSuccess, $sDesc);
|
||||
return Livetrail::getResult($bSuccess, $sLangId, array('subscribe'=>$bSubscribe));
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
@@ -207,7 +209,7 @@ class User extends PhpObject {
|
||||
$this->clearSession();
|
||||
$this->clearCookie();
|
||||
$this->setUserId(0);
|
||||
return Livetrail::getResult(true, 'lang:account.logged_out');
|
||||
return Livetrail::getResult(true, 'account.logged_out');
|
||||
}
|
||||
|
||||
public function updateNickname($sNickname) {
|
||||
@@ -226,19 +228,23 @@ class User extends PhpObject {
|
||||
|
||||
public function setUserClearance($iUserId, $iClearance) {
|
||||
$bSuccess = false;
|
||||
$sDesc = '';
|
||||
$sLangId = '';
|
||||
$asLangParams = array();
|
||||
|
||||
if(!$this->checkUserClearance(self::CLEARANCE_ADMIN)) $sDesc = 'unauthorized';
|
||||
if(!$this->checkUserClearance(self::CLEARANCE_ADMIN)) $sLangId = 'error.no_auth';
|
||||
else {
|
||||
if(!in_array($iClearance, self::CLEARANCES)) $sDesc = 'Setting wrong clearance "'.$iClearance.'" to user ID "'.$iUserId.'"';
|
||||
if(!in_array($iClearance, self::CLEARANCES)) {
|
||||
$sLangId = 'error.impossible_value';
|
||||
$asLangParams = array($iClearance, 'clearance');
|
||||
}
|
||||
else {
|
||||
$iUserId = $this->oDb->updateRow(self::USER_TABLE, $iUserId, array('clearance'=>$iClearance));
|
||||
if(!$iUserId) $sDesc = 'lang:error.commit_db';
|
||||
if(!$iUserId) $sLangId = 'error.commit_db';
|
||||
else $bSuccess = true;
|
||||
}
|
||||
}
|
||||
|
||||
return Livetrail::getResult($bSuccess, $sDesc);
|
||||
return Livetrail::getResult($bSuccess, $sLangId, array(), $asLangParams);
|
||||
}
|
||||
|
||||
/* Session */
|
||||
|
||||
Reference in New Issue
Block a user