Permalink
Browse files

New /admin/people endpoint for listing users & groups.

  • Loading branch information...
1 parent 445d2f7 commit 3c50dbb86fc66cb27524853fce0937df0d2c88f3 @cdujeu cdujeu committed Jul 14, 2016
Oops, something went wrong.
@@ -75,6 +75,11 @@ public function configureRoutes(\FastRoute\RouteCollector &$r){
}
+ /**
+ * Get Path component of the URI, without query parameters
+ * @param ServerRequestInterface $request
+ * @return string
+ */
public function getURIForRequest(ServerRequestInterface $request){
$uri = $request->getServerParams()['REQUEST_URI'];
@@ -85,6 +90,12 @@ public function getURIForRequest(ServerRequestInterface $request){
return rawurldecode($uri);
}
+ /**
+ * Find a route in api definitions
+ * @param ServerRequestInterface $request
+ * @param ResponseInterface $response
+ * @return bool
+ */
public function route(ServerRequestInterface &$request, ResponseInterface &$response){
$dispatcher = \FastRoute\cachedDispatcher(function(\FastRoute\RouteCollector $r) {
@@ -137,14 +148,21 @@ public function route(ServerRequestInterface &$request, ResponseInterface &$resp
return false;
}
+ /**
+ * Analyze URI and parameters to guess the current workspace
+ *
+ * @param ServerRequestInterface $request
+ * @param array $pathVars
+ * @return mixed|string
+ */
protected function findRepositoryInParameters(ServerRequestInterface $request, array $pathVars){
$params = array_merge($request->getParsedBody(), $pathVars);
if(isSet($params["workspaceId"])){
return $params["workspaceId"];
- }else if(isSet($params["path"]) && strpos($params["path"], "/") !== false){
- return array_shift(explode("/", ltrim($params["path"], "/")));
}else if (preg_match('/^\/admin\//', $request->getAttribute("api_uri"))) {
return "ajxp_conf";
+ }else if(isSet($params["path"]) && strpos($params["path"], "/") !== false){
+ return array_shift(explode("/", ltrim($params["path"], "/")));
}
// If no repo ID was found, return default repo id "pydio".
return "pydio";
@@ -163,6 +163,34 @@ protected function getMainTree(ContextInterface $ctx){
/**
* @param ServerRequestInterface $requestInterface
* @param ResponseInterface $responseInterface
+ * @return bool
+ */
+ public function preprocessLsApi2(ServerRequestInterface &$requestInterface, ResponseInterface &$responseInterface){
+
+ $uri = $requestInterface->getAttribute("api_uri");
+ $vars = $requestInterface->getParsedBody();
+ if($uri === "/admin/roles") {
+
+ $vars["dir"] = "/data/roles";
+ $requestInterface = $requestInterface->withParsedBody($vars);
+
+ }else if(strpos($uri, "/admin/people") === 0){
+
+ $crtPath = "";
+ if(isSet($vars["path"]) && !empty($vars["path"]) && $vars["path"] !== "/"){
+ $crtPath = $vars["path"];
+ }
+ $vars["dir"] = "/data/users".$crtPath;
+ $requestInterface = $requestInterface->withParsedBody($vars);
+
+ }
+
+
+ }
+
+ /**
+ * @param ServerRequestInterface $requestInterface
+ * @param ResponseInterface $responseInterface
*/
public function listAction(ServerRequestInterface $requestInterface, ResponseInterface &$responseInterface){
@@ -172,11 +200,22 @@ public function listAction(ServerRequestInterface $requestInterface, ResponseInt
}
if($requestInterface->getAttribute("api") === "v2"){
+
$uri = $requestInterface->getAttribute("api_uri");
$vars = $requestInterface->getParsedBody();
if($uri === "/admin/roles") {
$vars["dir"] = "/data/roles";
$requestInterface = $requestInterface->withParsedBody($vars);
+
+ }else if(strpos($uri, "/admin/people") === 0){
+
+ $crtPath = "";
+ if(isSet($vars["path"]) && !empty($vars["path"]) && $vars["path"] !== "/"){
+ $crtPath = $vars["path"];
+ }
+ $vars["dir"] = "/data/users".$crtPath;
+ $requestInterface = $requestInterface->withParsedBody($vars);
+
}
}
@@ -280,7 +280,7 @@ public function rolesActions(ServerRequestInterface $requestInterface, ResponseI
}
// Make sure it's utf8
- $data["ALL"] = [
+ $data["ALL"] = array_merge($data["ALL"], [
"PLUGINS_SCOPES" => [
"GLOBAL_TYPES" => ["conf", "auth", "authfront", "log", "mq", "notifications", "gui", "sec"],
"GLOBAL_PLUGINS" => ["action.avatar", "action.disclaimer", "action.scheduler", "action.skeleton", "action.updater"]
@@ -294,7 +294,7 @@ public function rolesActions(ServerRequestInterface $requestInterface, ResponseI
"shared|".$mess["ajxp_conf.158"],
"guest|".$mess["ajxp_conf.159"]
]
- ];
+ ]);
$scope = "role";
if($roleGroup) {
@@ -608,6 +608,15 @@ public function listNodes(ServerRequestInterface $requestInterface, $rootPath, $
"is_file" => false,
"text" => ""
]);
+ if(isSet($requestInterface->getParsedBody()["format"])){
+ $format = $requestInterface->getParsedBody()["format"];
+ }else if($requestInterface->getAttribute("api") === "v2"){
+ $format = "json";
+ }else{
+ $format = "xml";
+ }
+
+
$nodesList->setParentNode($parentNode);
$baseGroup = ($relativePath === "users" ? "/" : substr($relativePath, strlen("users")));
@@ -678,17 +687,35 @@ public function listNodes(ServerRequestInterface $requestInterface, $rootPath, $
}
+ if($format === "json" && $baseGroup !== "/"){
+
+ $siblingGroups = UsersService::listChildrenGroups(dirname($baseGroup));
+ $gKey = "/".basename($baseGroup);
+ $baseGroupLabel = isset($siblingGroups[$gKey]) ? $siblingGroups[$gKey] : null;
+ if(!count($users) && !count($groups) && $baseGroupLabel === null){
+ // Group does not seem to exist. Maybe we are getting info about a user here
+ try{
+ $testUser = UsersService::getUserById(basename($baseGroup));
+ if($testUser->getGroupPath() === dirname($baseGroup)){
+ $userMeta = $this->serializeUserMetadata($testUser, $format, $messages);
+ $nodesList->setParentNode(new AJXP_Node($testUser->getId(), $userMeta));
+ return $nodesList;
+ }
+ } catch (UserNotFoundException $unf){}
+ } else if($baseGroupLabel !== null) {
+ $parentNode = new AJXP_Node($baseGroup, $this->serializeGroupMetadata($baseGroup, $baseGroupLabel));
+ $nodesList->setParentNode($parentNode);
+ }
+
+ }
+
+
// Append Root Group
if($this->pluginName === "ajxp_admin" && $baseGroup == "/" && $paginationHash == 1 && !$this->currentUserIsGroupAdmin()){
- $rootGroupNode = new AJXP_Node($fullBasePath ."/", [
- "icon" => "users-folder.png",
- "icon_class" => "icon-home",
- "ajxp_mime" => "group",
- "object_id" => "/",
- "is_file" => false,
- "text" => $messages["ajxp_conf.151"]
- ]);
+ $topMeta = $this->serializeGroupMetadata("/", $messages["ajxp_conf.151"]);
+ $topMeta["icon_class"] = "icon-home";
+ $rootGroupNode = new AJXP_Node($fullBasePath ."/", $topMeta);
$nodesList->addBranch($rootGroupNode);
}
@@ -697,15 +724,12 @@ public function listNodes(ServerRequestInterface $requestInterface, $rootPath, $
foreach ($groups as $groupId => $groupLabel) {
$nodeKey = $fullBasePath ."/".ltrim($groupId,"/");
- $meta = array(
- "icon" => "users-folder.png",
- "icon_class" => "icon-folder-close",
- "ajxp_mime" => "group",
- "object_id" => $groupId,
- "text" => $groupLabel,
- "is_file" => false
- );
+ $meta = $this->serializeGroupMetadata($groupId, $groupLabel);
$this->appendBookmarkMeta($nodeKey, $meta);
+ if($requestInterface->getAttribute("api") === "v2"){
+ $meta["group_role_id"] = "/AJXP_GRP_".rtrim($baseGroup, "/")."/".ltrim($groupId, "/");
+ $nodeKey = InputFilter::securePath("/".$baseGroup.$groupId);
+ }
$nodesList->addBranch(new AJXP_Node($nodeKey, $meta));
}
@@ -731,9 +755,20 @@ public function listNodes(ServerRequestInterface $requestInterface, $rootPath, $
if(isSet($allUserIds) && count($allUserIds)){
$connections = $logger->usersLastConnection($allUserIds);
}
+
ksort($userArray);
+ /** @var UserInterface $userObject */
foreach ($userArray as $userObject) {
+
+ $userId = $userObject->getId();
+ $bmKey = $fullBasePath. "/" .$userId;
+ $nodeKey = $format === "json" ? $userId : $bmKey;
+ $meta = $this->serializeUserMetadata($userObject, $format, $messages, $connections);
+ $this->appendBookmarkMeta($bmKey, $meta);
+ $nodesList->addBranch(new AJXP_Node($nodeKey, $meta));
+
+ /*
$repos = ConfService::getConfStorageImpl()->listRepositories($userObject);
$isAdmin = $userObject->isAdmin();
$userId = $userObject->getId();
@@ -761,10 +796,17 @@ public function listNodes(ServerRequestInterface $requestInterface, $rootPath, $
$nodeKey = $fullBasePath. "/" .$userId;
$roles = array_filter(array_keys($userObject->getRoles()), array($this, "filterReservedRoles"));
$mergedRole = $userObject->mergedRole->getDataArray(true);
- if(!isSet($requestInterface["format"]) || $requestInterface["format"] !== "json"){
+ $meta = [];
+ if($format !== "json"){
$mergedRole = json_encode($mergedRole);
+ $currentRoles = implode(", ", $roles);
+ $jsonKey = $nodeKey;
+ }else{
+ $currentRoles = $roles;
+ $meta["personal_role_id"] = "/AJXP_USR_/".$userId;
+ $jsonKey = $userId;
}
- $meta = [
+ $meta = array_merge($meta, [
"text" => $nodeLabel,
"is_file" => true,
"isAdmin" => $messages[($isAdmin?"ajxp_conf.14":"ajxp_conf.15")],
@@ -773,24 +815,110 @@ public function listNodes(ServerRequestInterface $requestInterface, $rootPath, $
"object_id" => $userId,
"auth_scheme" => ($scheme != null? $scheme : ""),
"rights_summary" => $rightsString,
- "ajxp_roles" => implode(", ", $roles),
+ "ajxp_roles" => $currentRoles,
"ajxp_mime" => "user".(($userId!="guest"&&$userId!=$this->context->getUser()->getId())?"_editable":""),
"json_merged_role" => $mergedRole
- ];
+ ]);
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"] = StatHelper::relativeDate($meta["last_connection"], $messages);
}
+
$this->appendBookmarkMeta($nodeKey, $meta);
- $nodesList->addBranch(new AJXP_Node($nodeKey, $meta));
+ $nodesList->addBranch(new AJXP_Node($jsonKey, $meta));
+ */
}
return $nodesList;
}
/**
+ * @param string $groupId
+ * @param string $groupLabel
+ * @return array
+ */
+ protected function serializeGroupMetadata($groupId, $groupLabel){
+ return [
+ "icon" => "users-folder.png",
+ "icon_class" => "icon-folder-close",
+ "ajxp_mime" => "group",
+ "object_id" => $groupId,
+ "text" => $groupLabel,
+ "is_file" => false
+ ];
+ }
+
+ /**
+ * @param UserInterface $userObject
+ * @param string $format
+ * @param array $messages
+ * @param array
+ * @return array
+ */
+ protected function serializeUserMetadata($userObject, $format, $messages, $connections = []){
+
+ $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 = $messages["ajxp_conf.63"];
+ } else {
+ $r = array();
+ foreach ($repos as $repoId => $repository) {
+ if($repository->getAccessType() == "ajxp_shared") continue;
+ if(!$userObject->canRead($repoId) && !$userObject->canWrite($repoId)) continue;
+ $rs = ($userObject->canRead($repoId) ? "r" : "");
+ $rs .= ($userObject->canWrite($repoId) ? "w" : "");
+ $r[] = $repository->getDisplay()." (".$rs.")";
+ }
+ $rightsString = implode(", ", $r);
+ }
+ $nodeLabel = UsersService::getUserPersonalParameter("USER_DISPLAY_NAME", $userObject, "core.conf", $userId);
+ $scheme = UsersService::getAuthScheme($userId);
+ $roles = array_filter(array_keys($userObject->getRoles()), array($this, "filterReservedRoles"));
+ $mergedRole = $userObject->getMergedRole()->getDataArray(true);
+ $meta = [];
+ if($format !== "json"){
+ $mergedRole = json_encode($mergedRole);
+ $currentRoles = implode(", ", $roles);
+ }else{
+ $currentRoles = $roles;
+ $meta["personal_role_id"] = "/AJXP_USR_/".$userId;
+ }
+ $meta = array_merge($meta, [
+ "text" => $nodeLabel,
+ "is_file" => true,
+ "isAdmin" => $messages[($isAdmin?"ajxp_conf.14":"ajxp_conf.15")],
+ "icon" => $icon.".png",
+ "icon_class" => $iconClass,
+ "object_id" => $userId,
+ "auth_scheme" => ($scheme != null? $scheme : ""),
+ "rights_summary" => $rightsString,
+ "ajxp_roles" => $currentRoles,
+ "ajxp_mime" => "user".(($userId!="guest"&&$userId!=$this->context->getUser()->getId())?"_editable":""),
+ "json_merged_role" => $mergedRole
+ ]);
+ 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"] = StatHelper::relativeDate($meta["last_connection"], $messages);
+ }
+
+ return $meta;
+
+ }
+
+ /**
* Do not display AJXP_GRP_/ and AJXP_USR_/ roles if not in server debug mode
* @param $key
* @return bool
@@ -600,6 +600,9 @@ public function getPath()
return !empty($this->urlParts["path"]) ? $this->urlParts["path"] : "/";
}
+ /**
+ * @return bool
+ */
public function isRoot()
{
return !isset($this->urlParts["path"]) || $this->urlParts["path"] == "/";
@@ -794,6 +797,9 @@ function jsonSerialize()
$data = $this->_metadata;
unset($data["filename"]);
$data["path"] = $this->_metadata["filename"];
+ if(empty($data["path"]) && !empty($this->urlParts["path"])){
+ $data["path"] = $this->urlParts["path"];
+ }
if(isSet($this->_metadata["is_file"])){
unset($data["is_file"]);
$data["type"] = $this->isLeaf() ? "leaf" : "collection";

0 comments on commit 3c50dbb

Please sign in to comment.