public static bool RequestPermission(string userToRequestName, out string ErrorMessage) { if (userPublic.AuthorizedPiList.Any(ap => ap.PiID == userToRequestName)) { ErrorMessage = $"You already have permission to scan {userToRequestName}'s home"; return(false); } IQueryable <UserPublic> userPublicQueryable = _documentClient.CreateDocumentQuery <UserPublic>( PublicUsersCollectionUri).Where(f => f.Id == userToRequestName); if (userPublicQueryable.Count() == 0) { ErrorMessage = $"You cannot request the permission to scan {userToRequestName}'s house because this user does not exist..."; return(false); } UserPublic userToRequest = null; foreach (UserPublic u in userPublicQueryable) { userToRequest = u; } if (userToRequest == null) { ErrorMessage = StringConstants.SomethingWentWrongScanPermissionErrorMessage; return(false); } ScanRequest scanRequest = new ScanRequest(userPublic.Id, userPublic.FullName); if (userToRequest.ScanRequestList.Any(u => u.UsernameRequester == userPublic.Id)) { ErrorMessage = $"You have already requested {userToRequestName} to scan their home"; return(false); } userToRequest.ScanRequestList.Add(scanRequest); try { _documentClient.ReplaceDocumentAsync(PublicUserDocumentUri(userToRequestName), userToRequest); } catch { ErrorMessage = StringConstants.SomethingWentWrongScanPermissionErrorMessage; return(false); } ErrorMessage = $"A request was sent to {userToRequestName}!"; return(true); }
public static bool UpdateUserPublic(InputType type, out string ErrorMessage) { Uri documentUri = PublicUserDocumentUri(); if (documentUri == null) { ErrorMessage = StringConstants.UserDataNotLoadedErrorMessage; return(false); } if (type == InputType.MacAddress && !InputValidation.ValidateMacAddress(userPublic.MacAddress, out ErrorMessage)) { return(false); } _documentClient.ReplaceDocumentAsync(documentUri, userPublic); if (type == InputType.FullName) { IQueryable <UserPublic> userPublicQueryable = _documentClient.CreateDocumentQuery <UserPublic>( PublicUsersCollectionUri).SelectMany(u => u.AuthorizedPiList.Where(ap => ap.PiID == userPublic.Id).Select(ap => u)); foreach (UserPublic user in userPublicQueryable) { AuthorizedPi pi = user.AuthorizedPiList.Find(ap => ap.PiID == userPublic.Id); pi.FullNameOwner = userPublic.FullName; _documentClient.ReplaceDocumentAsync(PublicUserDocumentUri(user.Id), user); } userPublicQueryable = _documentClient.CreateDocumentQuery <UserPublic>( PublicUsersCollectionUri).SelectMany(u => u.ScanRequestList.Where(sr => sr.UsernameRequester == userPublic.Id).Select(sr => u)); foreach (UserPublic user in userPublicQueryable) { ScanRequest scanRequest = user.ScanRequestList.Find(sr => sr.UsernameRequester == userPublic.Id); scanRequest.FullNameRequester = userPublic.FullName; _documentClient.ReplaceDocumentAsync(PublicUserDocumentUri(user.Id), user); } } ErrorMessage = $"{type} {StringConstants.UpdatedSuccessfullyMessage}"; return(true); }
public static bool ProcessPermissionsToRequester(ScanRequest scanRequest, bool acceptRequest, bool updateOwnUser, out string ErrorMessage) { ErrorMessage = string.Empty; if (!userPublic.ScanRequestList.Contains(scanRequest)) { ErrorMessage = "This request does not exist"; return(false); } bool returnStatus = true; if (acceptRequest) { IQueryable <UserPublic> userPublicQueryable = _documentClient.CreateDocumentQuery <UserPublic>( PublicUsersCollectionUri).Where(f => f.Id == scanRequest.UsernameRequester); do { if (userPublicQueryable.Count() == 0) { ErrorMessage = $"Cannot find the user requesting permission"; returnStatus = false; break; } UserPublic userRequesting = null; foreach (UserPublic u in userPublicQueryable) { userRequesting = u; } if (userRequesting.AuthorizedPiList.Any(ap => ap.PiID == userPublic.Id)) { ErrorMessage = $"{userRequesting.Id} already has permission to scan your home"; returnStatus = false; break; } AuthorizedPi authorization = new AuthorizedPi(userPublic.Id, userPublic.FullName, string.Empty); userRequesting.AuthorizedPiList.Add(authorization); _documentClient.ReplaceDocumentAsync(PublicUserDocumentUri(userRequesting.Id), userRequesting); }while(false); } userPublic.ScanRequestList.Remove(scanRequest); if (updateOwnUser) { UpdateUserPublic(InputType.Username, out string message); } if (returnStatus == true) { switch (acceptRequest) { case true: ErrorMessage = $"Successfully accepted {scanRequest.UsernameRequester}'s request"; break; case false: ErrorMessage = $"Denied {scanRequest.UsernameRequester}'s request to scan your home"; break; } } return(returnStatus); }