Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
New function usersLastConnection on sqlLogger.
Browse files Browse the repository at this point in the history
New sub_action users_bulk_update_roles to avoid multiple query
  • Loading branch information
cdujeu committed Aug 26, 2015
1 parent 63c1e55 commit 1cea9ba
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
60 changes: 57 additions & 3 deletions core/src/plugins/access.ajxp_conf/class.ajxp_confAccessDriver.php
Expand Up @@ -643,14 +643,14 @@ public function switchAction($action, $httpVars, $fileVars)
"ALL" => array(
"PLUGINS_SCOPES" => array("GLOBAL_TYPES" => array("conf", "auth", "authfront", "log", "mq", "notifications", "gui")),
"REPOSITORIES" => $repos,
"REPOSITORIES_DETAILS" => $repoDetailed
"REPOSITORIES_DETAILS" => $repoDetailed,
"PROFILES" => array("standard|Standard","admin|Administrator","shared|Shared","guest|Guest")
)
);
if (isSet($userObject)) {
$data["USER"] = array();
$data["USER"]["LOCK"] = $userObject->getLock();
$data["USER"]["PROFILE"] = $userObject->getProfile();
$data["ALL"]["PROFILES"] = array("standard|Standard","admin|Administrator","shared|Shared","guest|Guest");
$data["USER"]["ROLES"] = array_keys($userObject->getRoles());
$data["ALL"]["ROLES"] = array_keys(AuthService::getRolesList(array(), true));
if (isSet($userObject->parentRole)) {
Expand Down Expand Up @@ -1010,6 +1010,45 @@ public function switchAction($action, $httpVars, $fileVars)

break;

case "users_bulk_update_roles":

$data = json_decode($httpVars["json_data"], true);
$userIds = $data["users"];
$rolesOperations = $data["roles"];
foreach($userIds as $userId){
$userId = AJXP_Utils::sanitize($userId, AJXP_SANITIZE_EMAILCHARS);
if(!AuthService::userExists($userId)) continue;
$userObject = ConfService::getConfStorageImpl()->createUserObject($userId);
if(!AuthService::canAdministrate($userObject)) continue;
foreach($rolesOperations as $addOrRemove => $roles){
if(!in_array($addOrRemove, array("add", "remove"))) {
continue;
}
foreach($roles as $roleId){
if(strpos($roleId, "AJXP_USR_/") === 0 || strpos($roleId,"AJXP_GRP_/" === 0)){
continue;
}
$roleId = AJXP_Utils::sanitize($roleId, AJXP_SANITIZE_FILENAME);
if ($addOrRemove == "add") {
$roleObject = AuthService::getRole($roleId);
$userObject->addRole($roleObject);
} else {
$userObject->removeRole($roleId);
}
}
}
$userObject->save("superuser");
$loggedUser = AuthService::getLoggedUser();
if ($loggedUser->getId() == $userObject->getId()) {
AuthService::updateUser($userObject);
}
}
AJXP_XMLWriter::header();
AJXP_XMLWriter::sendMessage("Successfully updated roles", null);
AJXP_XMLWriter::close();

break;

case "user_update_role" :

$confStorage = ConfService::getConfStorageImpl();
Expand Down Expand Up @@ -2092,8 +2131,13 @@ public function listUsers($root, $child, $hashValue = null, $returnNodes = false
$mess = ConfService::getMessages();
$loggedUser = AuthService::getLoggedUser();
$userArray = array();
$logger = AJXP_Logger::getInstance();
if(method_exists($logger, "usersLastConnection")){
$allUserIds = array();
}
foreach ($users as $userObject) {
$label = $userObject->getId();
if(isSet($allUserIds)) $allUserIds[] = $label;
if ($userObject->hasParent()) {
$label = $userObject->getParent()."000".$label;
}else{
Expand All @@ -2104,14 +2148,19 @@ public function listUsers($root, $child, $hashValue = null, $returnNodes = false
}
$userArray[$label] = $userObject;
}
if(isSet($allUserIds)){
$connections = $logger->usersLastConnection($allUserIds);
}
ksort($userArray);
foreach ($userArray as $userObject) {
$repos = ConfService::getConfStorageImpl()->listRepositories($userObject);
$isAdmin = $userObject->isAdmin();
$userId = $userObject->getId();
$icon = "user".($userId=="guest"?"_guest":($isAdmin?"_admin":""));
$iconClass = "icon-user";
if ($userObject->hasParent()) {
$icon = "user_child";
$iconClass = "icon-angle-right";
}
if ($isAdmin) {
$rightsString = $mess["ajxp_conf.63"];
Expand All @@ -2135,14 +2184,19 @@ public function listUsers($root, $child, $hashValue = null, $returnNodes = false
$meta = array(
"isAdmin" => $mess[($isAdmin?"ajxp_conf.14":"ajxp_conf.15")],
"icon" => $icon.".png",
"icon_class" => "icon-user",
"icon_class" => $iconClass,
"object_id" => $userId,
"auth_scheme" => ($scheme != null? $scheme : ""),
"rights_summary" => $rightsString,
"ajxp_roles" => implode(", ", $roles),
"ajxp_mime" => "user".(($userId!="guest"&&$userId!=$loggedUser->getId())?"_editable":""),
"json_merged_role" => json_encode($userObject->mergedRole->getDataArray(true))
);
if($userObject->hasParent()) $meta["shared_user"] = "true";
if(isSet($connections) && isSet($connections[$userObject->getId()]) && !empty($connections[$userObject->getId()])) {
$meta["last_connection"] = strtotime($connections[$userObject->getId()]);
$meta["last_connection_readable"] = AJXP_Utils::relativeDate($meta["last_connection"], $mess);
}
if(in_array($nodeKey, $this->currentBookmarks)) $meta = array_merge($meta, array("ajxp_bookmarked" => "true", "overlay_icon" => "bookmark.png"));
$xml = AJXP_XMLWriter::renderNode($nodeKey, $nodeLabel, true, $meta, true, false);
if(!$returnNodes) print($xml);
Expand Down
6 changes: 6 additions & 0 deletions core/src/plugins/log.sql/class.sqlLogDriver.php
Expand Up @@ -65,6 +65,12 @@ public function performChecks()
}
}

public function usersLastConnection($userIds){
$res = dibi::query("SELECT [user], MAX([logdate]) AS date_max FROM [ajxp_log] WHERE [user] IN (%s) GROUP BY [user]", $userIds);
$all = $res->fetchPairs("user", "date_max");
return $all;
}

public function exposeQueries($actionName, &$httpVars, &$fileVars){

header('Content-type: application/json');
Expand Down

0 comments on commit 1cea9ba

Please sign in to comment.