authsql : add the language support

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2055 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-03-23 12:02:00 +00:00
parent 25b3fce39e
commit 07d0707445

View File

@@ -34,16 +34,20 @@ class authsql extends auth
public function connect ()
{
if (! function_exists ("openssl_random_pseudo_bytes"))
throw new Exception ("No PHP support for openssl_random_pseudo_bytes",
throw new Exception (dgettext("domframework",
"No PHP support for openssl_random_pseudo_bytes"),
500);
$this->db = new dblayer ($this->dsn, $this->username, $this->password,
$this->driver_options);
if ($this->table === null)
throw new Exception ("No SQL table defined", 500);
throw new Exception (dgettext("domframework","No SQL table defined"),
500);
if ($this->fieldIdentifier === null)
throw new Exception ("No fieldIdentifier defined", 500);
throw new Exception (dgettext("domframework",
"No fieldIdentifier defined"), 500);
if ($this->fieldPassword === null)
throw new Exception ("No fieldPassword defined", 500);
throw new Exception (dgettext("domframework",
"No fieldPassword defined"), 500);
$fields = array_merge (array ($this->fieldIdentifier, $this->fieldPassword),
$this->fieldsInfo);
$fields = array_flip ($fields);
@@ -60,19 +64,25 @@ class authsql extends auth
public function authentication ($email, $password)
{
if ($this->db === null)
throw new Exception ("The SQL database is not connected", 500);
throw new Exception (dgettext("domframework",
"The SQL database is not connected"), 500);
$data = $this->db->read (array (array ($this->fieldIdentifier, $email)),
array_merge (array ($this->fieldIdentifier,
$this->fieldPassword),
$this->fieldsInfo));
if (count ($data) === 0)
throw new Exception ("Unable to find the user : '$email'", 401);
throw new Exception (sprintf (dgettext("domframework",
"Unable to find the user : '%s'"),
$email), 401);
if (! isset ($data[0][$this->fieldPassword]))
throw new Exception ("Unable to get the user password from database",
throw new Exception (dgettext("domframework",
"Unable to get the user password from database"),
500);
$cryptedPassword = $data[0][$this->fieldPassword];
if (crypt ($password, $cryptedPassword) !== $cryptedPassword)
throw new Exception ("Bad password for '$email'", 401);
throw new Exception (sprintf (dgettext("domframework",
"Bad password for '%s'"), $email),
401);
unset ($data[0][$this->fieldPassword]);
$this->details = $data[0];
}
@@ -90,12 +100,13 @@ class authsql extends auth
public function changepassword ($oldpassword, $newpassword)
{
if ($this->db === null)
throw new Exception ("The SQL database is not connected", 500);
throw new Exception (dgettext("domframework",
"The SQL database is not connected"), 500);
if ($this->details === null ||
! isset ($this->details[$this->fieldIdentifier]))
throw new Exception (
"Can't change the password if the user is not authenticated",
500);
throw new Exception (dgettext("domframework",
"Can't change the password if the user is not authenticated"),
500);
$data = $this->db->read (array (array ($this->fieldIdentifier,
$this->details[$this->fieldIdentifier])),
array_merge (array ($this->fieldIdentifier,
@@ -103,7 +114,8 @@ class authsql extends auth
$this->fieldsInfo));
$cryptedPassword = $data[0][$this->fieldPassword];
if (crypt ($oldpassword, $cryptedPassword) !== $cryptedPassword)
throw new Exception ("Bad old password provided", 401);
throw new Exception (dgettext("domframework",
"Bad old password provided"), 401);
$cost = 11;
$salt=substr(base64_encode(openssl_random_pseudo_bytes(17)),0,22);
$salt=str_replace("+",".",$salt);
@@ -117,7 +129,8 @@ class authsql extends auth
$rc = $this->db->update ($this->details[$this->fieldIdentifier],
array ($this->fieldPassword => $cryptpassword));
if ($rc !== 1)
throw new Exception ("Can't change the password", 500);
throw new Exception (dgettext("domframework",
"Can't change the password"), 500);
}
/** Method to overwrite the password (without oldpassword check)
@@ -128,13 +141,16 @@ class authsql extends auth
public function overwritepassword ($email, $newpassword)
{
if ($this->db === null)
throw new Exception ("The SQL database is not connected", 500);
throw new Exception (dgettext("domframework",
"The SQL database is not connected"), 500);
$data = $this->db->read (array (array ($this->fieldIdentifier, $email)),
array_merge (array ($this->fieldIdentifier,
$this->fieldPassword),
$this->fieldsInfo));
if (count ($data) === 0)
throw new Exception ("Unable to find the user : '$email'", 401);
throw new Exception (sprintf (dgettext("domframework",
"Unable to find the user : '%s'"),
$email), 401);
$cost = 11;
$salt=substr(base64_encode(openssl_random_pseudo_bytes(17)),0,22);
$salt=str_replace("+",".",$salt);
@@ -148,14 +164,16 @@ class authsql extends auth
$rc = $this->db->update ($email,
array ($this->fieldPassword => $cryptpassword));
if ($rc !== 1)
throw new Exception ("Can't change the password", 500);
throw new Exception (dgettext("domframework","Can't change the password"),
500);
}
/** List all the users available in the database
Return firstname, lastname, mail, with mail is an array */
public function listusers ()
{
if ($this->db === null)
throw new Exception ("The SQL database is not connected", 500);
throw new Exception (dgettext("domframework",
"The SQL database is not connected"), 500);
$data = $this->db->read (null, array_merge (array ($this->fieldIdentifier),
$this->fieldsInfo));
return $data;