method __construct documentation in bbn\User
User constructor.
function(bbn\Db $db, array $params = [], array $cfg = [])
{
// The database connection
$this->db = $db;
// Setting up the class configuration
$this->_init_class_cfg($cfg);
$f = &$this->class_cfg['fields'];
self::retrieverInit($this);
if ($this->isToken() && !empty($params[$f['token']])) {
if ($this->isPhoneNumberCodeSendingRequest($params)) {
// Verify that the received token is associated with the device uid
if (!($user_id = $this->getUserByTokenAndDeviceUid($params[$f['token']], $params[$f['device_uid']]))) {
$this->setError(20);
return $this->api_request_output = [
'success' => false,
'error' => X::_('Invalid token'),
'errorCode' => 20
];
}
// Check if the phone number is already registered
if (($exUser = $this->findByPhoneNumber($params[$f['phone_number']]))
&& ($exUser[$f['id']] !== $user_id)
&& $this->updateApiTokenUserByTokenDevice(
$params[$f['token']],
$params[$f['device_uid']],
$exUser[$f['id']],
!empty($params[$f['device_lang']]) ? str_replace('"', '', $params[$f['device_lang']]) : ''
)
) {
if (!$this->db->selectOne($this->class_cfg['table'], $this->class_cfg['arch']['users']['login'], [
$this->class_cfg['arch']['users']['id'] => $user_id
])) {
$this->db->delete($this->class_cfg['table'], [
$this->class_cfg['arch']['users']['id'] => $user_id
]);
}
$user_id = $exUser[$f['id']];
}
$this->id = $user_id;
// Generate a code
$code = random_int(1001, 9999);
try {
$phone = \Brick\PhoneNumber\PhoneNumber::parse($params[$f['phone_number']]);
} catch (\Brick\PhoneNumber\PhoneNumberParseException $e) {
$this->setError(21);
return $this->api_request_output = [
'success' => false,
'error' => X::_('Invalid phone number'),
'errorCode' => 21
];
}
if (
!$this->hasSkipVerification()
&& !$phone->isValidNumber()
) {
$this->setError(21);
return $this->api_request_output = [
'success' => false,
'error' => X::_('Invalid phone number'),
'errorCode' => 21
];
}
// Save it
if ($this->updatePhoneVerificationCode($params[$f['phone_number']], $code)) {
// Send the sms with code here
return $this->api_request_output = [
'success' => true,
'phone_verification_code' => $code
];
} else {
$this->setError(22);
return [
'success' => false,
'error' => X::_('Impossible to update the phone number or the verification code'),
'errorCode' => 22
];
}
} elseif ($this->isVerifyPhoneNumberRequest($params)) {
// Verify that the received token is associated to the device uid
if (!$this->verifyTokenAndDeviceUid($params[$f['device_uid']], $params[$f['token']])) {
$this->setError(20);
return $this->api_request_output = [
'success' => false,
'error' => X::_('Invalid token'),
'errorCode' => 20
];
}
// find the user using phone_number in db
$user = $this->findByPhoneNumber($params[$f['phone_number']]);
if (!$user) {
$this->setError(23);
return $this->api_request_output = [
'success' => false,
'error' => X::_('Unknown phone number'),
'errorCode' => 23
];
}
$this->id = $user[$this->class_cfg['arch']['users']['id']];
$this->id_group = $user[$this->class_cfg['arch']['users']['id_group']];
if (!$this->hasSkipVerification()) {
// Verify that the code is correct
$user_cgf = json_decode($user[$this->class_cfg['arch']['users']['cfg']], true);
if (
!$user_cgf
|| !isset($user_cgf['phone_verification_code'])
|| ((string)$user_cgf['phone_verification_code'] !== (string)$params[$f['phone_verification_code']])
) {
$this->setError(24);
return $this->api_request_output = [
'success' => false,
'error' => X::_('Invalid verification code'),
'errorCode' => 24
];
}
}
// Update verification code to null
$this->updatePhoneVerificationCode($params[$f['phone_number']], null);
// Generate a new token
$new_token = Str::genpwd(32, 16);
// Update user id and the new token in the row with the old token and device uid.
$this->db->update(
$this->class_cfg['tables']['api_tokens'],
[
$this->class_cfg['arch']['api_tokens']['id_user'] => $user[$this->class_cfg['arch']['users']['id']],
$this->class_cfg['arch']['api_tokens']['token'] => $new_token,
],
[
$this->class_cfg['arch']['api_tokens']['token'] => $params[$f['token']],
$this->class_cfg['arch']['api_tokens']['device_uid'] => $params[$f['device_uid']],
]
);
// Send the new token here
return $this->api_request_output = [
'token' => $new_token,
'success' => true
];
} elseif ($this->isTokenLoginRequest($params)) {
// Find the token associated to the device uid in db then get it's associated user.
if (!$user = $this->findUserByApiTokenAndDeviceUid($params[$f['token']], $params[$f['device_uid']])) {
$this->setError(20);
return $this->api_request_output = [
'success' => false,
'error' => X::_('Invalid token'),
'errorCode' => 20
];
}
// Update device_lang and last
$toUdp = [
$this->class_cfg['arch']['api_tokens']['last'] => date('Y-m-d H:i:S')
];
if (isset($params[$f['device_lang']])) {
$toUdp[$this->class_cfg['arch']['api_tokens']['device_lang']] = $params[$f['device_lang']];
}
$this->db->update($this->class_cfg['tables']['api_tokens'], $toUdp, [
$this->class_cfg['arch']['api_tokens']['token'] => $params[$f['token']],
$this->class_cfg['arch']['api_tokens']['device_uid'] => $params[$f['device_uid']]
]);
// Now the user is authenticated
$this->auth = true;
$this->id = $user[$this->class_cfg['arch']['users']['id']];
$this->id_group = $user[$this->class_cfg['arch']['users']['id_group']];
return $this->api_request_output = [
'token' => $params[$f['token']],
'success' => true
];
}
} else {
// The client environment variables
$this->user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$this->ip_address = $this->class_cfg['ip_address'] && isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$this->accept_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
if (empty($this->user_agent)) {
X::log([X::isCli(), $_SERVER], 'user_sess');
}
// Creating the session's variables if they don't exist yet
$this->_init_session();
// CLI user
if (x::isCli() && isset($params['id'])) {
$this->id = $params['id'];
$this->auth = true;
}
// The user logs in
if ($this->isLoginRequest($params)) {
/** @todo separate credentials and salt checking */
if (
!empty($this->sess_cfg['fingerprint'])
&& $this->getPrint($this->_get_session('fingerprint')) === $this->sess_cfg['fingerprint']
) {
/** @todo separate credentials and salt checking */
$this->_check_credentials($params);
} else {
$this->setError(19);
$this->session->destroy();
}
}
/** @todo revise the process: dying is not the solution! */
// The user is not known yet
elseif ($this->isResetPasswordRequest($params)) {
if ($id = $this->getIdFromMagicString($params[$f['id']], $params[$f['key']])) {
$this->password_reset = true;
if (($params[$f['pass1']] === $params[$f['pass2']])) {
$this->expireHotlink($params[$f['id']]);
$this->id = $id;
$this->forcePassword($params[$f['pass2']]);
$this->session->set([]);
} else {
$this->setError(7);
}
} else {
$this->setError(18);
}
} else {
$this->checkSession();
}
}
}
User constructor. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions