Update user query and user adapter

This commit is contained in:
Romain Neutron
2012-09-21 14:43:23 +02:00
parent 28880af8e5
commit a757adbdbe
4 changed files with 212 additions and 242 deletions

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* @package User * @package User
@@ -103,9 +105,9 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
/** /**
* *
* @var appbox * @var Application
*/ */
protected $appbox; protected $app;
/** /**
* *
@@ -215,12 +217,6 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
*/ */
protected $company; protected $company;
/**
*
* @var boolean
*/
protected $is_admin;
/** /**
* *
* @var boolean * @var boolean
@@ -317,9 +313,10 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
* @param appbox $appbox * @param appbox $appbox
* @return User_Adapter * @return User_Adapter
*/ */
public function __construct($id, appbox &$appbox) public function __construct($id, Application $app)
{ {
$this->appbox = $appbox;
$this->app = $app;
$this->load($id); $this->load($id);
return $this; return $this;
@@ -328,23 +325,23 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
/** /**
* *
* @param type $id * @param type $id
* @param appbox $appbox * @param Application $app
* @return User_Adapter * @return User_Adapter
*/ */
public static function getInstance($id, appbox &$appbox) public static function getInstance($id, Application $app)
{ {
if (is_int((int) $id) && (int) $id > 0) { if (is_int((int) $id) && (int) $id > 0) {
$id = (int) $id; $id = (int) $id;
} else } else
throw new Exception('Invalid usr_id'); throw new Exception('Invalid usr_id');
if ( ! isset(self::$_instance[$id])) { if (!isset(self::$_instance[$id])) {
try { try {
self::$_instance[$id] = $appbox->get_data_from_cache('_user_' . $id); self::$_instance[$id] = $app['phraseanet.appbox']->get_data_from_cache('_user_' . $id);
self::$_instance[$id]->set_appbox($appbox); self::$_instance[$id]->set_app($app['phraseanet.appbox']);
} catch (Exception $e) { } catch (Exception $e) {
self::$_instance[$id] = new self($id, $appbox); self::$_instance[$id] = new self($id, $app);
$appbox->set_data_to_cache(self::$_instance[$id], '_user_' . $id); $app['phraseanet.appbox']->set_data_to_cache(self::$_instance[$id], '_user_' . $id);
} }
} }
@@ -363,11 +360,11 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
/** /**
* *
* @param appbox $appbox * @param Application $app
*/ */
protected function set_appbox(appbox &$appbox) protected function set_app(Application $app)
{ {
$this->appbox = $appbox; $this->app = $app;
} }
/** /**
@@ -380,9 +377,9 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$sql = 'UPDATE usr SET usr_password = :password, salted_password = "1" $sql = 'UPDATE usr SET usr_password = :password, salted_password = "1"
WHERE usr_id = :usr_id'; WHERE usr_id = :usr_id';
$password = self::salt_password($pasword, $this->get_nonce()); $password = self::salt_password($this->app, $pasword, $this->get_nonce());
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':password' => $password, ':usr_id' => $this->get_id())); $stmt->execute(array(':password' => $password, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
@@ -400,14 +397,14 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$email = null; $email = null;
} }
$test_user = User_Adapter::get_usr_id_from_email($email); $test_user = User_Adapter::get_usr_id_from_email($this->app, $email);
if ($test_user && $test_user != $this->get_id()) { if ($test_user && $test_user != $this->get_id()) {
throw new Exception_InvalidArgument(sprintf(_('A user already exists with email addres %s'), $email)); throw new Exception_InvalidArgument(sprintf(_('A user already exists with email addres %s'), $email));
} }
$sql = 'UPDATE usr SET usr_mail = :new_email WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET usr_mail = :new_email WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':new_email' => $email, ':usr_id' => $this->get_id())); $stmt->execute(array(':new_email' => $email, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->email = $email; $this->email = $email;
@@ -423,8 +420,9 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
*/ */
protected function get_ACL() protected function get_ACL()
{ {
if ( ! $this->ACL instanceof ACL) if (!$this->ACL instanceof ACL) {
$this->ACL = new ACL($this, $this->appbox); $this->ACL = new ACL($this, $this->app);
}
return $this->ACL; return $this->ACL;
} }
@@ -436,26 +434,25 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
*/ */
public function get_protected_rss_url($renew = false) public function get_protected_rss_url($renew = false)
{ {
$session = $this->appbox->get_session(); $registry = $this->app['phraseanet.appbox']->get_registry();
$registry = $this->appbox->get_registry();
$token = $title = false; $token = $title = false;
if ( ! $renew) { if (!$renew) {
$sql = 'SELECT value FROM tokens WHERE usr_id = :usr_id AND type="rss"'; $sql = 'SELECT value FROM tokens WHERE usr_id = :usr_id AND type="rss"';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
$token = $row['value']; $token = $row['value'];
} else { } else {
$sql = 'DELETE FROM tokens WHERE usr_id = :usr_id AND type="rss"'; $sql = 'DELETE FROM tokens WHERE usr_id = :usr_id AND type="rss"';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
} }
if ($token === false) { if ($token === false) {
$token = random::getUrlToken(\random::TYPE_RSS, $this->id); $token = random::getUrlToken($this->app, \random::TYPE_RSS, $this->id);
} }
return new system_url($registry->get('GV_ServerName') . 'atom/' . $token); return new system_url($registry->get('GV_ServerName') . 'atom/' . $token);
@@ -467,19 +464,17 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
* @param unknown_type $query * @param unknown_type $query
* @return boolean * @return boolean
*/ */
public static function saveQuery($query) public static function saveQuery(Application $app, $query)
{ {
try { try {
$appbox = appbox::get_instance(\bootstrap::getCore()); $user = $app['phraseanet.user'];
$session = $appbox->get_session();
$user = User_Adapter::getInstance($session->get_usr_id(), $appbox);
$sql = "INSERT INTO dsel (id, name, usr_id, query) $sql = "INSERT INTO dsel (id, name, usr_id, query)
VALUES (null, :name, :usr_id, :query)"; VALUES (null, :name, :usr_id, :query)";
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array( $stmt->execute(array(
':name' => $query, ':name' => $query,
':usr_id' => $session->get_usr_id(), ':usr_id' => $app['phraseanet.user']->get_id(),
':query' => $query ':query' => $query
)); ));
$stmt->closeCursor(); $stmt->closeCursor();
@@ -500,9 +495,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function get_country() public function get_country()
{ {
if ($this->geonameid) { if ($this->geonameid) {
$geonames = new geonames(); return $this->app['geonames']->get_country($this->geonameid);
return $geonames->get_country($this->geonameid);
} }
return ''; return '';
@@ -513,9 +506,9 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
* @param string $login * @param string $login
* @return int * @return int
*/ */
public static function get_usr_id_from_login($login) public static function get_usr_id_from_login(Application $app, $login)
{ {
$conn = connection::getPDOConnection(); $conn = connection::getPDOConnection($app);
$sql = 'SELECT usr_id FROM usr WHERE usr_login = :login'; $sql = 'SELECT usr_id FROM usr WHERE usr_login = :login';
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
$stmt->execute(array(':login' => trim($login))); $stmt->execute(array(':login' => trim($login)));
@@ -535,7 +528,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_defaultftpdatas($datas) public function set_defaultftpdatas($datas)
{ {
$sql = 'UPDATE usr SET defaultftpdatasent = :defaultftpdatas WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET defaultftpdatasent = :defaultftpdatas WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':defaultftpdatas' => $datas, ':usr_id' => $this->get_id())); $stmt->execute(array(':defaultftpdatas' => $datas, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->defaultftpdatas = $datas; $this->defaultftpdatas = $datas;
@@ -552,10 +545,10 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
{ {
$value = $boolean ? '1' : '0'; $value = $boolean ? '1' : '0';
$sql = 'UPDATE usr SET mail_notifications = :mail_notifications WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET mail_notifications = :mail_notifications WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':mail_notifications' => $value, ':usr_id' => $this->get_id())); $stmt->execute(array(':mail_notifications' => $value, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->mail_notifications = ! ! $boolean; $this->mail_notifications = !!$boolean;
$this->delete_data_from_cache(); $this->delete_data_from_cache();
return $this; return $this;
@@ -570,7 +563,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
{ {
$value = $boolean ? '1' : '0'; $value = $boolean ? '1' : '0';
$sql = 'UPDATE usr SET activeftp = :activeftp WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET activeftp = :activeftp WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':activeftp' => $value, ':usr_id' => $this->get_id())); $stmt->execute(array(':activeftp' => $value, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->activeftp = $boolean; $this->activeftp = $boolean;
@@ -587,7 +580,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
{ {
$value = $boolean ? '1' : '0'; $value = $boolean ? '1' : '0';
$sql = 'UPDATE usr SET ldap_created = :ldap_created WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET ldap_created = :ldap_created WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':ldap_created' => $value, ':usr_id' => $this->get_id())); $stmt->execute(array(':ldap_created' => $value, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->ldap_created = $boolean; $this->ldap_created = $boolean;
@@ -603,7 +596,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_ftp_address($address) public function set_ftp_address($address)
{ {
$sql = 'UPDATE usr SET addrftp = :addrftp WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET addrftp = :addrftp WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':addrftp' => $address, ':usr_id' => $this->get_id())); $stmt->execute(array(':addrftp' => $address, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->ftp_address = $address; $this->ftp_address = $address;
@@ -620,7 +613,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_ftp_login($login) public function set_ftp_login($login)
{ {
$sql = 'UPDATE usr SET loginftp = :loginftp WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET loginftp = :loginftp WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':loginftp' => $login, ':usr_id' => $this->get_id())); $stmt->execute(array(':loginftp' => $login, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->ftp_login = $login; $this->ftp_login = $login;
@@ -636,7 +629,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_ftp_password($password) public function set_ftp_password($password)
{ {
$sql = 'UPDATE usr SET pwdFTP = :passwordftp WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET pwdFTP = :passwordftp WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':passwordftp' => $password, ':usr_id' => $this->get_id())); $stmt->execute(array(':passwordftp' => $password, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->ftp_password = $password; $this->ftp_password = $password;
@@ -649,10 +642,10 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
{ {
$value = $boolean ? '1' : '0'; $value = $boolean ? '1' : '0';
$sql = 'UPDATE usr SET passifftp = :passifftp WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET passifftp = :passifftp WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':passifftp' => $value, ':usr_id' => $this->get_id())); $stmt->execute(array(':passifftp' => $value, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->ftp_passif = ! ! $boolean; $this->ftp_passif = !!$boolean;
return $this; return $this;
} }
@@ -660,7 +653,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_ftp_dir($ftp_dir) public function set_ftp_dir($ftp_dir)
{ {
$sql = 'UPDATE usr SET destftp = :destftp WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET destftp = :destftp WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':destftp' => $ftp_dir, ':usr_id' => $this->get_id())); $stmt->execute(array(':destftp' => $ftp_dir, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->ftp_dir = $ftp_dir; $this->ftp_dir = $ftp_dir;
@@ -672,7 +665,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_ftp_dir_prefix($ftp_dir_prefix) public function set_ftp_dir_prefix($ftp_dir_prefix)
{ {
$sql = 'UPDATE usr SET prefixFTPfolder = :prefixftp WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET prefixFTPfolder = :prefixftp WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':prefixftp' => $ftp_dir_prefix, ':usr_id' => $this->get_id())); $stmt->execute(array(':prefixftp' => $ftp_dir_prefix, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->ftp_dir_prefix = $ftp_dir_prefix; $this->ftp_dir_prefix = $ftp_dir_prefix;
@@ -683,7 +676,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_firstname($firstname) public function set_firstname($firstname)
{ {
$sql = 'UPDATE usr SET usr_prenom = :usr_prenom WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET usr_prenom = :usr_prenom WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_prenom' => $firstname, ':usr_id' => $this->get_id())); $stmt->execute(array(':usr_prenom' => $firstname, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->firstname = $firstname; $this->firstname = $firstname;
@@ -695,7 +688,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_lastname($lastname) public function set_lastname($lastname)
{ {
$sql = 'UPDATE usr SET usr_nom = :usr_nom WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET usr_nom = :usr_nom WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_nom' => $lastname, ':usr_id' => $this->get_id())); $stmt->execute(array(':usr_nom' => $lastname, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->lastname = $lastname; $this->lastname = $lastname;
@@ -707,7 +700,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_address($address) public function set_address($address)
{ {
$sql = 'UPDATE usr SET adresse = :adresse WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET adresse = :adresse WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':adresse' => $address, ':usr_id' => $this->get_id())); $stmt->execute(array(':adresse' => $address, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->address = $address; $this->address = $address;
@@ -719,7 +712,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_city($city) public function set_city($city)
{ {
$sql = 'UPDATE usr SET ville = :city WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET ville = :city WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':city' => $city, ':usr_id' => $this->get_id())); $stmt->execute(array(':city' => $city, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->city = $city; $this->city = $city;
@@ -730,8 +723,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_geonameid($geonameid) public function set_geonameid($geonameid)
{ {
$geonames = new geonames(); $country_code = $this->app['geonames']->get_country_code($geonameid);
$country_code = $geonames->get_country_code($geonameid);
$sql = 'UPDATE usr SET geonameid = :geonameid, pays=:country_code WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET geonameid = :geonameid, pays=:country_code WHERE usr_id = :usr_id';
$datas = array( $datas = array(
@@ -740,7 +732,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
':country_code' => $country_code ':country_code' => $country_code
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($datas); $stmt->execute($datas);
$stmt->closeCursor(); $stmt->closeCursor();
$this->geonameid = $geonameid; $this->geonameid = $geonameid;
@@ -753,7 +745,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_zip($zip) public function set_zip($zip)
{ {
$sql = 'UPDATE usr SET cpostal = :cpostal WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET cpostal = :cpostal WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':cpostal' => $zip, ':usr_id' => $this->get_id())); $stmt->execute(array(':cpostal' => $zip, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->zip = $zip; $this->zip = $zip;
@@ -765,7 +757,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_gender($gender) public function set_gender($gender)
{ {
$sql = 'UPDATE usr SET usr_sexe = :usr_sexe WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET usr_sexe = :usr_sexe WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_sexe' => $gender, ':usr_id' => $this->get_id())); $stmt->execute(array(':usr_sexe' => $gender, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->gender = $gender; $this->gender = $gender;
@@ -777,7 +769,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_tel($tel) public function set_tel($tel)
{ {
$sql = 'UPDATE usr SET tel = :tel WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET tel = :tel WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':tel' => $tel, ':usr_id' => $this->get_id())); $stmt->execute(array(':tel' => $tel, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->tel = $tel; $this->tel = $tel;
@@ -789,7 +781,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_fax($fax) public function set_fax($fax)
{ {
$sql = 'UPDATE usr SET fax = :fax WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET fax = :fax WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':fax' => $fax, ':usr_id' => $this->get_id())); $stmt->execute(array(':fax' => $fax, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->fax = $fax; $this->fax = $fax;
@@ -801,7 +793,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_job($job) public function set_job($job)
{ {
$sql = 'UPDATE usr SET fonction = :fonction WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET fonction = :fonction WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':fonction' => $job, ':usr_id' => $this->get_id())); $stmt->execute(array(':fonction' => $job, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->job = $job; $this->job = $job;
@@ -813,7 +805,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_position($position) public function set_position($position)
{ {
$sql = 'UPDATE usr SET activite = :activite WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET activite = :activite WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':activite' => $position, ':usr_id' => $this->get_id())); $stmt->execute(array(':activite' => $position, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->position = $position; $this->position = $position;
@@ -825,7 +817,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_company($company) public function set_company($company)
{ {
$sql = 'UPDATE usr SET societe = :company WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET societe = :company WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':company' => $company, ':usr_id' => $this->get_id())); $stmt->execute(array(':company' => $company, ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$this->company = $company; $this->company = $company;
@@ -843,7 +835,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
throw new Exception_InvalidArgument (); throw new Exception_InvalidArgument ();
$sql = 'UPDATE usr SET model_of = :owner_id WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET model_of = :owner_id WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':owner_id' => $owner->get_id(), ':usr_id' => $this->get_id())); $stmt->execute(array(':owner_id' => $owner->get_id(), ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
@@ -889,13 +881,13 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return $this->template_owner; return $this->template_owner;
} }
public static function get_usr_id_from_email($email) public static function get_usr_id_from_email(Application $app, $email)
{ {
if (is_null($email)) { if (is_null($email)) {
return false; return false;
} }
$conn = connection::getPDOConnection(); $conn = connection::getPDOConnection($app);
$sql = 'SELECT usr_id FROM usr $sql = 'SELECT usr_id FROM usr
WHERE usr_mail = :email WHERE usr_mail = :email
AND usr_login NOT LIKE "(#deleted_%" AND usr_login NOT LIKE "(#deleted_%"
@@ -918,57 +910,57 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
{ {
$sql = 'UPDATE usr SET usr_login = :usr_login , usr_mail = null $sql = 'UPDATE usr SET usr_login = :usr_login , usr_mail = null
WHERE usr_id = :usr_id'; WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_login' => '(#deleted_' . $this->get_login() . '_' . $this->get_id(), ':usr_id' => $this->get_id())); $stmt->execute(array(':usr_login' => '(#deleted_' . $this->get_login() . '_' . $this->get_id(), ':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM basusr WHERE usr_id = :usr_id'; $sql = 'DELETE FROM basusr WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM sbasusr WHERE usr_id = :usr_id'; $sql = 'DELETE FROM sbasusr WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM dsel WHERE usr_id = :usr_id'; $sql = 'DELETE FROM dsel WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM edit_presets WHERE usr_id = :usr_id'; $sql = 'DELETE FROM edit_presets WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM ftp_export WHERE usr_id = :usr_id'; $sql = 'DELETE FROM ftp_export WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM `order` WHERE usr_id = :usr_id'; $sql = 'DELETE FROM `order` WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM sselnew WHERE usr_id = :usr_id'; $sql = 'DELETE FROM sselnew WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM tokens WHERE usr_id = :usr_id'; $sql = 'DELETE FROM tokens WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM usr_settings WHERE usr_id = :usr_id'; $sql = 'DELETE FROM usr_settings WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = 'DELETE FROM ssel WHERE usr_id = :usr_id'; $sql = 'DELETE FROM ssel WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
@@ -1036,12 +1028,16 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
pwdFTP, passifftp, destftp, prefixFTPfolder, mail_locked, model_of pwdFTP, passifftp, destftp, prefixFTPfolder, mail_locked, model_of
FROM usr WHERE usr_id= :id '; FROM usr WHERE usr_id= :id ';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':id' => $id)); $stmt->execute(array(':id' => $id));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
if (!$row) {
throw new \Exception('User unknown');
}
$this->id = (int) $row['usr_id']; $this->id = (int) $row['usr_id'];
$this->email = $row['usr_mail']; $this->email = $row['usr_mail'];
$this->login = $row['usr_login']; $this->login = $row['usr_login'];
@@ -1058,7 +1054,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$this->ftp_dir = $row['destftp']; $this->ftp_dir = $row['destftp'];
$this->ftp_dir_prefix = $row['prefixFTPfolder']; $this->ftp_dir_prefix = $row['prefixFTPfolder'];
$this->mail_locked = ! ! $row['mail_locked']; $this->mail_locked = !!$row['mail_locked'];
$this->firstname = $row['usr_prenom']; $this->firstname = $row['usr_prenom'];
$this->lastname = $row['usr_nom']; $this->lastname = $row['usr_nom'];
@@ -1076,14 +1072,13 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$this->modificationdate = new DateTime($row['usr_modificationdate']); $this->modificationdate = new DateTime($row['usr_modificationdate']);
$this->applied_template = $row['lastModel']; $this->applied_template = $row['lastModel'];
$this->country = geonames::get_country($row['geonameid']); $this->country = $this->app['geonames']->get_country($row['geonameid']);
$this->is_admin = ($row['create_db'] == '1');
$this->is_guest = ($row['invite'] == '1'); $this->is_guest = ($row['invite'] == '1');
if ($row['model_of'] > 0) { if ($row['model_of'] > 0) {
$this->is_template = true; $this->is_template = true;
$this->template_owner = self::getInstance($row['model_of'], $this->appbox); $this->template_owner = self::getInstance($row['model_of'], $this->app);
} }
return $this; return $this;
@@ -1098,7 +1093,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
, ':template_id' => $template->get_login() , ':template_id' => $template->get_login()
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
$this->delete_data_from_cache(); $this->delete_data_from_cache();
@@ -1109,10 +1104,10 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function set_mail_locked($boolean) public function set_mail_locked($boolean)
{ {
$sql = 'UPDATE usr SET mail_locked = :mail_locked WHERE usr_id = :usr_id'; $sql = 'UPDATE usr SET mail_locked = :mail_locked WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id(), ':mail_locked' => ($boolean ? '1' : '0'))); $stmt->execute(array(':usr_id' => $this->get_id(), ':mail_locked' => ($boolean ? '1' : '0')));
$stmt->closeCursor(); $stmt->closeCursor();
$this->mail_locked = ! ! $boolean; $this->mail_locked = !!$boolean;
return $this; return $this;
} }
@@ -1215,7 +1210,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
{ {
$sql = 'SELECT last_conn FROM usr WHERE usr_id = :usr_id'; $sql = 'SELECT last_conn FROM usr WHERE usr_id = :usr_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
@@ -1244,21 +1239,16 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return $this->modificationdate; return $this->modificationdate;
} }
public function is_admin()
{
return $this->is_admin;
}
protected function load_preferences() protected function load_preferences()
{ {
if ($this->_prefs) { if ($this->_prefs) {
return $this; return $this;
} }
$registry = \registry::get_instance(); $registry = $this->app['phraseanet.registry'];
$sql = 'SELECT prop, value FROM usr_settings WHERE usr_id= :id'; $sql = 'SELECT prop, value FROM usr_settings WHERE usr_id= :id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':id' => $this->id)); $stmt->execute(array(':id' => $this->id));
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -1268,7 +1258,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
} }
foreach (self::$def_values as $k => $v) { foreach (self::$def_values as $k => $v) {
if ( ! isset($this->_prefs[$k])) { if (!isset($this->_prefs[$k])) {
if ($k == 'start_page_query' && $registry->get('GV_defaultQuery')) { if ($k == 'start_page_query' && $registry->get('GV_defaultQuery')) {
$v = $registry->get('GV_defaultQuery'); $v = $registry->get('GV_defaultQuery');
} }
@@ -1281,15 +1271,15 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return $this; return $this;
} }
protected function load_notifications_preferences() protected function load_notifications_preferences(Application $app)
{ {
$Core = bootstrap::getCore();
$evt_mngr = $Core['events-manager']; $evt_mngr = $app['events-manager'];
$notifications = $evt_mngr->list_notifications_available($this->id); $notifications = $evt_mngr->list_notifications_available($this->id);
foreach ($notifications as $notification_group => $nots) { foreach ($notifications as $notification_group => $nots) {
foreach ($nots as $notification) { foreach ($nots as $notification) {
if ( ! isset($this->_prefs['notification_' . $notification['id']])) { if (!isset($this->_prefs['notification_' . $notification['id']])) {
$this->_prefs['notification_' . $notification['id']] = '1'; $this->_prefs['notification_' . $notification['id']] = '1';
$this->update_pref('notification_' . $notification['id'], '1'); $this->update_pref('notification_' . $notification['id'], '1');
@@ -1300,18 +1290,18 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
} }
protected $notifications_preferences_loaded = false; protected $notifications_preferences_loaded = false;
public function get_notifications_preference($notification_id) public function get_notifications_preference(Application $app, $notification_id)
{ {
if ( ! $this->notifications_preferences_loaded) if (!$this->notifications_preferences_loaded)
$this->load_notifications_preferences(); $this->load_notifications_preferences($app);
return $this->_prefs['notification_' . $notification_id]; return $this->_prefs['notification_' . $notification_id];
} }
public function set_notification_preference($notification_id, $value) public function set_notification_preference(Application $app, $notification_id, $value)
{ {
if ( ! $this->notifications_preferences_loaded) if (!$this->notifications_preferences_loaded)
$this->load_notifications_preferences(); $this->load_notifications_preferences($app);
return $this->_prefs['notification_' . $notification_id] = $value ? '1' : '0'; return $this->_prefs['notification_' . $notification_id] = $value ? '1' : '0';
} }
@@ -1336,7 +1326,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$sql = 'REPLACE INTO usr_settings (usr_id, prop, value) $sql = 'REPLACE INTO usr_settings (usr_id, prop, value)
VALUES (:usr_id, :prop, :value)'; VALUES (:usr_id, :prop, :value)';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array( $stmt->execute(array(
':usr_id' => $this->id, ':usr_id' => $this->id,
':prop' => $prop, ':prop' => $prop,
@@ -1357,21 +1347,21 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function delete_data_from_cache($option = null) public function delete_data_from_cache($option = null)
{ {
$this->appbox->delete_data_from_cache($this->get_cache_key($option)); $this->app['phraseanet.appbox']->delete_data_from_cache($this->get_cache_key($option));
return $this; return $this;
} }
public function get_data_from_cache($option = null) public function get_data_from_cache($option = null)
{ {
$this->appbox->get_data_from_cache($this->get_cache_key($option)); $this->app['phraseanet.appbox']->get_data_from_cache($this->get_cache_key($option));
return $this; return $this;
} }
public function set_data_to_cache($value, $option = null, $duration = 0) public function set_data_to_cache($value, $option = null, $duration = 0)
{ {
$this->appbox->set_data_to_cache($value, $this->get_cache_key($option), $duration); $this->app['phraseanet.appbox']->set_data_to_cache($value, $this->get_cache_key($option), $duration);
return $this; return $this;
} }
@@ -1386,10 +1376,10 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
if (substr($file, 0, 1) == "." || strtolower($file) == "cvs") if (substr($file, 0, 1) == "." || strtolower($file) == "cvs")
continue; continue;
if (is_dir($path . "/" . $file) && strpos($file, '_') == 2 && strlen($file) == 5) { if (is_dir($path . "/" . $file) && strpos($file, '_') == 2 && strlen($file) == 5) {
if ( ! array_key_exists($file, self::$locales)) if (!array_key_exists($file, self::$locales))
continue; continue;
$supFile = explode('_', $file); $supFile = explode('_', $file);
if ( ! isset($lngs[$supFile[0]])) if (!isset($lngs[$supFile[0]]))
$lngs[$supFile[0]] = array(); $lngs[$supFile[0]] = array();
$lngs[$supFile[0]][$file] = array('name' => self::$locales[$file], 'selected' => false); $lngs[$supFile[0]][$file] = array('name' => self::$locales[$file], 'selected' => false);
} }
@@ -1399,12 +1389,12 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return $lngs; return $lngs;
} }
public static function get_wrong_email_users(appbox $appbox) public static function get_wrong_email_users(Application $app)
{ {
$sql = 'SELECT usr_mail, usr_id FROM usr WHERE usr_mail IS NOT NULL'; $sql = 'SELECT usr_mail, usr_id FROM usr WHERE usr_mail IS NOT NULL';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(); $stmt->execute();
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -1414,7 +1404,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$users = array(); $users = array();
foreach ($rs as $row) { foreach ($rs as $row) {
if ( ! isset($users[$row['usr_mail']])) { if (!isset($users[$row['usr_mail']])) {
$users[$row['usr_mail']] = array(); $users[$row['usr_mail']] = array();
} }
@@ -1427,7 +1417,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
if (count($usrs) > 1) { if (count($usrs) > 1) {
$bad_users[$email] = array(); $bad_users[$email] = array();
foreach ($usrs as $usr_id) { foreach ($usrs as $usr_id) {
$user = User_Adapter::getInstance($usr_id, $appbox); $user = User_Adapter::getInstance($usr_id, $app);
$bad_users[$email][$user->get_id()] = $user; $bad_users[$email][$user->get_id()] = $user;
} }
} }
@@ -1464,7 +1454,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
public function getPrefs($prop) public function getPrefs($prop)
{ {
$this->load_preferences(); $this->load_preferences();
if ( ! isset($this->_prefs[$prop])) { if (!isset($this->_prefs[$prop])) {
$this->_prefs[$prop] = null; $this->_prefs[$prop] = null;
$this->update_pref($prop, null); $this->update_pref($prop, null);
} }
@@ -1472,18 +1462,27 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return $this->_prefs[$prop]; return $this->_prefs[$prop];
} }
public static function updateClientInfos($app_id) public static function updateClientInfos(Application $app, $app_id)
{ {
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = $app['phraseanet.appbox'];
$session = $appbox->get_session(); if (!$app->isAuthenticated()) {
if ( ! $session->is_authenticated()) {
return; return;
} }
$ses_id = $session->get_ses_id(); $session = $app['EM']->find('Entities\Session', $app['session']->get('session_id'));
$usr_id = $session->get_usr_id();
$user = User_Adapter::getInstance($usr_id, $appbox); if (!$session->hasModuleId($app_id)) {
$module = new \Entities\SessionModule();
$module->setModuleId($app_id);
$module->setSession($session);
$app['EM']->persist($module);
$app['EM']->persist($session);
}
$usr_id = $app['phraseanet.user']->get_id();
$user = User_Adapter::getInstance($usr_id, $app);
$appName = array( $appName = array(
'1' => 'Prod', '1' => 'Prod',
@@ -1502,20 +1501,20 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
foreach ($sbas_ids as $sbas_id) { foreach ($sbas_ids as $sbas_id) {
try { try {
$logger = $session->get_logger($appbox->get_databox($sbas_id)); $logger = $app['phraseanet.logger']($appbox->get_databox($sbas_id));
$connbas = connection::getPDOConnection($sbas_id); $connbas = connection::getPDOConnection($app, $sbas_id);
$sql = 'SELECT appli FROM log WHERE id = :log_id'; $sql = 'SELECT appli FROM log WHERE id = :log_id';
$stmt = $connbas->prepare($sql); $stmt = $connbas->prepare($sql);
$stmt->execute(array(':log_id' => $logger->get_id())); $stmt->execute(array(':log_id' => $logger->get_id()));
$row3 = $stmt->fetch(PDO::FETCH_ASSOC); $row3 = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
if ( ! $row3) if (!$row3)
throw new Exception('no log'); throw new Exception('no log');
$applis = unserialize($row3['appli']); $applis = unserialize($row3['appli']);
if ( ! in_array($app_id, $applis)) { if (!in_array($app_id, $applis)) {
$applis[] = $app_id; $applis[] = $app_id;
} }
@@ -1535,31 +1534,13 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
} }
} }
$sql = 'SELECT app FROM cache WHERE session_id = :ses_id';
$stmt = $appbox->get_connection()->prepare($sql);
$stmt->execute(array(':ses_id' => $ses_id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$apps = array();
if ($row)
$apps = unserialize($row['app']);
if ( ! in_array($app_id, $apps))
$apps[] = $app_id;
$sql = "UPDATE cache SET app = :applis WHERE session_id = :ses_id";
$stmt = $appbox->get_connection()->prepare($sql);
$stmt->execute(array(':applis' => serialize($apps), ':ses_id' => $ses_id));
$stmt->closeCursor();
return; return;
} }
public static function get_sys_admins() public static function get_sys_admins(Application $app)
{ {
$sql = 'SELECT usr_id, usr_login FROM usr WHERE create_db="1"'; $sql = 'SELECT usr_id, usr_login FROM usr WHERE create_db="1"';
$conn = connection::getPDOConnection(); $conn = connection::getPDOConnection($app);
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
$stmt->execute(); $stmt->execute();
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -1573,15 +1554,14 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return $users; return $users;
} }
public static function set_sys_admins($admins) public static function set_sys_admins(Application $app, $admins)
{ {
try { try {
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = $app['phraseanet.appbox'];
$session = $appbox->get_session();
$sql = "UPDATE usr SET create_db='0' WHERE create_db='1' AND usr_id != :usr_id"; $sql = "UPDATE usr SET create_db='0' WHERE create_db='1' AND usr_id != :usr_id";
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $appbox->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $session->get_usr_id())); $stmt->execute(array(':usr_id' => $app['phraseanet.user']->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
$sql = "UPDATE usr SET create_db='1' WHERE usr_id IN (" . implode(',', $admins) . ")"; $sql = "UPDATE usr SET create_db='1' WHERE usr_id IN (" . implode(',', $admins) . ")";
@@ -1597,15 +1577,15 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return false; return false;
} }
public static function reset_sys_admins_rights() public static function reset_sys_admins_rights(Application $app)
{ {
$users = self::get_sys_admins(); $users = self::get_sys_admins($app);
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = $app['phraseanet.appbox'];
foreach ($appbox->get_databoxes() as $databox) { foreach ($appbox->get_databoxes() as $databox) {
foreach (array_keys($users) as $usr_id) { foreach (array_keys($users) as $usr_id) {
$user = User_Adapter::getInstance($usr_id, $appbox); $user = User_Adapter::getInstance($usr_id, $app);
$user->ACL()->give_access_to_sbas(array($databox->get_sbas_id())); $user->ACL()->give_access_to_sbas(array($databox->get_sbas_id()));
$rights = array( $rights = array(
@@ -1650,34 +1630,24 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return; return;
} }
public static function get_locale($usr_id) public function get_locale()
{ {
$locale = 'en_GB'; $registry = $this->app['phraseanet.appbox']->get_registry();
try {
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();
$locale = $registry->get('GV_default_lng');
$sql = "SELECT locale FROM usr WHERE usr_id = :usr_id"; $sql = "SELECT locale FROM usr WHERE usr_id = :usr_id";
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $session->get_usr_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
if ($row) { $locale = $row['locale'] ? : $registry->get('GV_default_lng', 'en_GB');
$locale = $row['locale'];
}
} catch (Exception $e) {
}
return $locale; return $locale;
} }
public static function create(appbox &$appbox, $login, $password, $email, $admin, $invite = false) public static function create(Application $app, $login, $password, $email, $admin, $invite = false)
{ {
$conn = $appbox->get_connection(); $conn = $app['phraseanet.appbox']->get_connection();
if (trim($login) == '') { if (trim($login) == '') {
throw new \InvalidArgumentException('Invalid username'); throw new \InvalidArgumentException('Invalid username');
@@ -1699,7 +1669,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$stmt->execute(array( $stmt->execute(array(
':login' => $login, ':login' => $login,
':nonce' => $nonce, ':nonce' => $nonce,
':password' => self::salt_password($password, $nonce), ':password' => self::salt_password($app, $password, $nonce),
':email' => ($email ? $email : null), ':email' => ($email ? $email : null),
':admin' => ($admin ? '1' : '0'), ':admin' => ($admin ? '1' : '0'),
':invite' => ($invite ? '1' : '0') ':invite' => ($invite ? '1' : '0')
@@ -1715,12 +1685,12 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$stmt->closeCursor(); $stmt->closeCursor();
} }
return self::getInstance($usr_id, $appbox); return self::getInstance($usr_id, $app);
} }
public static function salt_password($password, $nonce) public static function salt_password(Application $app, $password, $nonce)
{ {
$registry = registry::get_instance(); $registry = $app['phraseanet.registry'];
return hash_hmac('sha512', $password . $nonce, $registry->get('GV_sit')); return hash_hmac('sha512', $password . $nonce, $registry->get('GV_sit'));
} }
@@ -1735,7 +1705,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
$nonce = false; $nonce = false;
$sql = 'SELECT nonce FROM usr WHERE usr_id = :usr_id '; $sql = 'SELECT nonce FROM usr WHERE usr_id = :usr_id ';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $this->get_id())); $stmt->execute(array(':usr_id' => $this->get_id()));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* @package User * @package User
@@ -20,9 +22,7 @@ interface User_Interface
public function get_id(); public function get_id();
public function is_admin(); public function __construct($id, Application $app);
public function __construct($id, appbox &$appbox);
public function ACL(); public function ACL();
@@ -138,9 +138,9 @@ interface User_Interface
public function get_creation_date(); public function get_creation_date();
public function get_notifications_preference($notification_id); public function get_notifications_preference(Application $app, $notification_id);
public function set_notification_preference($notification_id, $value); public function set_notification_preference(Application $app, $notification_id, $value);
public function get_display_name(); public function get_display_name();
@@ -152,25 +152,25 @@ interface User_Interface
public function getPrefs($prop); public function getPrefs($prop);
public static function updateClientInfos($app_id); public static function updateClientInfos(Application $app, $app_id);
public static function get_sys_admins(); public static function get_sys_admins(Application $app);
public static function set_sys_admins($admins); public static function set_sys_admins(Application $app, $admins);
public static function reset_sys_admins_rights(); public static function reset_sys_admins_rights(Application $app);
public static function get_locale($usr_id); public function get_locale();
public static function create(appbox &$appbox, $login, $password, $email, $admin, $invite = false); public static function create(Application $app, $login, $password, $email, $admin, $invite = false);
public static function salt_password($password, $nonce); public static function salt_password(Application $app, $password, $nonce);
public static function getInstance($id, appbox &$appbox); public static function getInstance($id, Application $app);
public static function saveQuery($query); public static function saveQuery(Application $app, $query);
public static function get_usr_id_from_login($login); public static function get_usr_id_from_login(Application $app, $login);
public static function get_usr_id_from_email($email); public static function get_usr_id_from_email(Application $app, $email);
} }

View File

@@ -9,6 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
/** /**
@@ -21,9 +22,9 @@ class User_Query implements User_QueryInterface
{ {
/** /**
* *
* @var appbox * @var Application
*/ */
protected $appbox; protected $app;
/** /**
* *
@@ -173,11 +174,11 @@ class User_Query implements User_QueryInterface
* *
* @return User_Query * @return User_Query
*/ */
public function __construct(appbox &$appbox) public function __construct(Application $app)
{ {
$this->appbox = $appbox; $this->app = $app;
foreach ($appbox->get_databoxes() as $databox) { foreach ($app['phraseanet.appbox']->get_databoxes() as $databox) {
$this->active_sbas[] = $databox->get_sbas_id(); $this->active_sbas[] = $databox->get_sbas_id();
foreach ($databox->get_collections() as $collection) { foreach ($databox->get_collections() as $collection) {
$this->active_bases[] = $collection->get_base_id(); $this->active_bases[] = $collection->get_base_id();
@@ -205,9 +206,6 @@ class User_Query implements User_QueryInterface
{ {
$this->sql_params = array(); $this->sql_params = array();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$sql = ' $sql = '
FROM usr LEFT JOIN basusr ON (usr.usr_id = basusr.usr_id) FROM usr LEFT JOIN basusr ON (usr.usr_id = basusr.usr_id)
LEFT JOIN sbasusr ON (usr.usr_id = sbasusr.usr_id) LEFT JOIN sbasusr ON (usr.usr_id = sbasusr.usr_id)
@@ -225,11 +223,11 @@ class User_Query implements User_QueryInterface
} }
if ($this->only_templates === true) { if ($this->only_templates === true) {
$sql .= ' AND model_of = ' . $session->get_usr_id(); $sql .= ' AND model_of = ' . $this->app['phraseanet.user']->get_id();
} elseif ($this->include_templates === false) { } elseif ($this->include_templates === false) {
$sql .= ' AND model_of=0'; $sql .= ' AND model_of=0';
} else { } else {
$sql .= ' AND (model_of=0 OR model_of = ' . $session->get_usr_id() . ' ) '; $sql .= ' AND (model_of=0 OR model_of = ' . $this->app['phraseanet.user']->get_id() . ' ) ';
} }
if ($this->activities) { if ($this->activities) {
@@ -459,7 +457,7 @@ class User_Query implements User_QueryInterface
*/ */
public function execute() public function execute()
{ {
$conn = $this->appbox->get_connection(); $conn = $this->app['phraseanet.appbox']->get_connection();
$sorter = array(); $sorter = array();
@@ -522,7 +520,7 @@ class User_Query implements User_QueryInterface
$users = new ArrayCollection(); $users = new ArrayCollection();
foreach ($rs as $row) { foreach ($rs as $row) {
$users[] = User_Adapter::getInstance($row['usr_id'], $this->appbox); $users[] = User_Adapter::getInstance($row['usr_id'], $this->app);
} }
$this->results = $users; $this->results = $users;
@@ -540,7 +538,7 @@ class User_Query implements User_QueryInterface
return $this->total; return $this->total;
} }
$conn = $this->appbox->get_connection(); $conn = $this->app['phraseanet.appbox']->get_connection();
$sql_count = 'SELECT COUNT(DISTINCT usr.usr_id) as total ' $sql_count = 'SELECT COUNT(DISTINCT usr.usr_id) as total '
. $this->generate_sql_constraints(); . $this->generate_sql_constraints();
@@ -883,7 +881,7 @@ class User_Query implements User_QueryInterface
public function getRelatedActivities() public function getRelatedActivities()
{ {
$conn = $this->appbox->get_connection(); $conn = $this->app['phraseanet.appbox']->get_connection();
$sql = 'SELECT DISTINCT usr.activite ' . $this->generate_sql_constraints(); $sql = 'SELECT DISTINCT usr.activite ' . $this->generate_sql_constraints();
@@ -908,7 +906,7 @@ class User_Query implements User_QueryInterface
public function getRelatedPositions() public function getRelatedPositions()
{ {
$conn = $this->appbox->get_connection(); $conn = $this->app['phraseanet.appbox']->get_connection();
$sql = 'SELECT DISTINCT usr.fonction ' . $this->generate_sql_constraints(); $sql = 'SELECT DISTINCT usr.fonction ' . $this->generate_sql_constraints();
@@ -935,7 +933,7 @@ class User_Query implements User_QueryInterface
{ {
require_once __DIR__ . '/../../classes/deprecated/countries.php'; require_once __DIR__ . '/../../classes/deprecated/countries.php';
$conn = $this->appbox->get_connection(); $conn = $this->app['phraseanet.appbox']->get_connection();
$sql = 'SELECT DISTINCT usr.pays ' . $this->generate_sql_constraints(); $sql = 'SELECT DISTINCT usr.pays ' . $this->generate_sql_constraints();
@@ -948,7 +946,7 @@ class User_Query implements User_QueryInterface
$pays = array(); $pays = array();
$ctry = \getCountries(\Session_Handler::get_locale()); $ctry = \getCountries($this->app['locale']);
foreach ($rs as $row) { foreach ($rs as $row) {
if (trim($row['pays']) === '') if (trim($row['pays']) === '')
@@ -963,7 +961,7 @@ class User_Query implements User_QueryInterface
public function getRelatedCompanies() public function getRelatedCompanies()
{ {
$conn = $this->appbox->get_connection(); $conn = $this->app['phraseanet.appbox']->get_connection();
$sql = 'SELECT DISTINCT usr.societe ' . $this->generate_sql_constraints(); $sql = 'SELECT DISTINCT usr.societe ' . $this->generate_sql_constraints();
@@ -988,7 +986,7 @@ class User_Query implements User_QueryInterface
public function getRelatedTemplates() public function getRelatedTemplates()
{ {
$conn = $this->appbox->get_connection(); $conn = $this->app['phraseanet.appbox']->get_connection();
$sql = 'SELECT DISTINCT usr.lastModel ' . $this->generate_sql_constraints(); $sql = 'SELECT DISTINCT usr.lastModel ' . $this->generate_sql_constraints();

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* @package User * @package User
@@ -18,7 +20,7 @@
interface User_QueryInterface interface User_QueryInterface
{ {
public function __construct(appbox &$appbox); public function __construct(Application $app);
public function get_results(); public function get_results();