private string FindMachineInstall(FindMachineInstallRequest req, IAaaUser aaaUser, ILogger logger) { IDictionary<string, FindUserEntity> userDetailDic = new Dictionary<string, FindUserEntity>(); req.Product = "est"; var findUserReq = new FindUserRequest { Filter = FindUserFilter.All, SearchString = req.SearchString, }; // For external user, the auto suggest will only show the users under the user's location scope var canOnlySeeYourOwnAccount = false; if (!Permission.IsUserInternal(aaaUser)) { var scope = Permission.GetTopLocationScope(aaaUser); if (!scope.Equals(default(KeyValuePair<FindLocationFilter, string>))) { findUserReq.LocationScope = scope; } else if (aaaUser.UserId.Contains(req.SearchString) || aaaUser.EmailAddress.Contains(req.SearchString) || aaaUser.UUID.Contains(req.SearchString) || aaaUser.FullName.Contains(req.SearchString)) { canOnlySeeYourOwnAccount = true; } else { logger.LogWarn("STOpsConsole - FindMachineInstall - external user {0} has no eligible scope.", aaaUser.UserId); return "{ \"items\":[]}"; } } using (var userInfoServiceclient = new UserInfoServiceClient(RouterBindings.Local, RouterAddresses.Local.RequestReply)) { var svcResp = userInfoServiceclient.FindUser(findUserReq); if (svcResp == null || svcResp.Users.Count == 0) { return "{ \"items\":[]}"; } var machInstReq = new MachInstInfoRequest { uuids = new List<string>(), filter = req.Filter, product = req.Product }; //If user can see only his own account due to the scope. Will filter out the list of find user. if (canOnlySeeYourOwnAccount) { var user = svcResp.Users.SingleOrDefault(x => x.Uuid == aaaUser.UUID); if (user != null) { userDetailDic[user.Uuid] = user; machInstReq.uuids.Add(user.Uuid); } else { return "{ \"items\":[]}"; } } else { foreach (FindUserEntity user in svcResp.Users) { userDetailDic[user.Uuid] = user; machInstReq.uuids.Add(user.Uuid); } } FindMachInstResponse findMachInstResponse = new FindMachInstResponse() { Items = new List<FindMachInstInfoItem>() }; using (var opsConsoleServiceClient = new OpsConsoleServiceClient(RouterBindings.Local, RouterAddresses.Local.RequestReply)) { MachInstInfoResponse machInstResp = opsConsoleServiceClient.GetMachineInstallInfo(machInstReq); foreach (MachInstInfoItem machInsInfo in machInstResp.Items) { FindMachInstInfoItem findMachInstInfoItem = new FindMachInstInfoItem { UUID = machInsInfo.uuid, FirstName = userDetailDic[machInsInfo.uuid].FirstName, LastName = userDetailDic[machInsInfo.uuid].LastName, EmailAddress = userDetailDic[machInsInfo.uuid].Email //MachInstInfoList = machInsInfo.machInstInfoList }; findMachInstResponse.Items.Add(findMachInstInfoItem); findMachInstResponse.Product = machInstResp.product; } } return JsonConvert.SerializeObject(findMachInstResponse) ?? "{}"; } }
public string GetUserScope(string query, string body, IAppServerServices services) { var logger = services.Logger; try { var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(body); var product = new List<string> { "est" }; var loginUser = values != null && values.ContainsKey("loginUser") ? values["loginUser"] : null; var lookedUpUser = values != null && values.ContainsKey("lookedUpUser") ? values["lookedUpUser"] : null; var searchString = values != null && values.ContainsKey("searchString") && !string.IsNullOrEmpty(values["searchString"]) ? values["searchString"] : "*"; var loginAaaUser = new AaaUser(); loginAaaUser.SetUser(!String.IsNullOrEmpty(loginUser) ? loginUser : services.UserContext.UUID); var fromCache = false; var cacheKey = string.Format("{0}_{1}_PEMISSION", services.UserContext.UUID, lookedUpUser ?? "INITIAL"); UserPermission userPermission; if (_Cache.Contains(cacheKey)) { userPermission = _Cache.Get(cacheKey) as UserPermission; fromCache = true; } else { userPermission = new UserPermission { IsAllowToUploadMetadata = Permission.IsAllowToUploadMetadata(loginAaaUser, product) ? "true" : "false", IsAllowToGetStats = Permission.IsAllowToGetStats(loginAaaUser, product) ? "true" : "false", IsInternal = Permission.IsUserInternal(loginAaaUser) ? "true" : "false", IsUserInScope = string.IsNullOrEmpty(lookedUpUser) ? "n/a" : Permission.IsUserInScope(loginAaaUser, lookedUpUser) ? "true" : "false" }; _Cache.Set(cacheKey, userPermission, DateTimeOffset.UtcNow.AddMinutes(1)); } #region loginUser var userSettingString = "{}"; var userPermissionSetting = Permission.GetUserPermission(loginAaaUser); if (userPermissionSetting != null) { userSettingString = JsonConvert.SerializeObject(userPermissionSetting); } var permissionString = "{}"; if (userPermission != null) { permissionString = JsonConvert.SerializeObject(userPermission); } var loginUserPermission = String.Format("{{\"userPermission\":{0},\"setting\":{1},\"cache\":\"{2}\"}}", permissionString, userSettingString, fromCache.ToString().ToLower() ); var loginUserLocation = String.Format("{{\"locationScope\":{0},\"topLocationScope\":\"{1}\"}}", Permission.GetUserInScopeAaaDetails(loginUser, loginUser), Permission.GetTopLocationScope(loginAaaUser) ); var loginUserResult = String.Format("{{\"Info\":{0},\"Permissions\":{1},\"Locations\":{2}}}", loginAaaUser.GetJsonString(), loginUserPermission, loginUserLocation ); #endregion #region searchUser var req = new FindMachineInstallRequest() { SearchString = searchString, Product = "est", Filter = true }; var searchResult = FindMachineInstall(req,loginAaaUser,logger); #endregion #region lookupUser var lookupUserResult = String.Format("{{\"locationScope\":\"empty argument\"}}"); var lookupAaaUser = new AaaUser(); if (!String.IsNullOrEmpty(lookedUpUser)) { var lookupUserLocation = String.Format("{{\"locationScope\":{0}}}", Permission.GetUserInScopeAaaDetails(loginUser, lookedUpUser) ); lookupAaaUser.SetUser(lookedUpUser); lookupUserResult = String.Format("{{\"Info\":{0},\"Locations\":{1}}}", lookupAaaUser.GetJsonString(), lookupUserLocation ); } #endregion var result = String.Format("{{\"LogInUser\":{0},\"LookUpUser\":{1},\"SearchResult\":{2},\"status\":\"{3}\"}}", loginUserResult, lookupUserResult, searchResult, "Done" ); return result; } catch (Exception ex) { logger.LogError("GetUserScope - error {0}", ex.Message); return String.Format("{{\"status\":\"{0}\"}}", "Error: " + ex.Message ); } }