Shared files expire and lock. Changed "Remove from organization" to delete.

This commit is contained in:
Jared Goodwin 2020-02-18 07:44:28 -08:00
parent 1e0a4ed389
commit 87a2f30166
9 changed files with 68 additions and 32 deletions

View File

@ -27,7 +27,8 @@ namespace Remotely.Server.API
public ActionResult Get(string id)
{
var sharedFile = DataService.GetSharedFiled(id);
if (sharedFile != null)
// Shared files expire after a minute and become locked.
if (sharedFile != null && sharedFile.Timestamp.AddMinutes(1) > DateTime.Now)
{
return File(sharedFile.FileContents, sharedFile.ContentType, sharedFile.FileName);
}

View File

@ -131,9 +131,9 @@ namespace Remotely.Server.API
return Ok(deviceGroupID);
}
[HttpDelete("RemoveUserFromOrganization/{userID}")]
[HttpDelete("DeleteUser/{userID}")]
[ServiceFilter(typeof(ApiAuthorizationFilter))]
public IActionResult RemoveUserFromOrganization(string userID)
public async Task<IActionResult> DeleteUser(string userID)
{
if (User.Identity.IsAuthenticated &&
!DataService.GetUserByName(User.Identity.Name).IsAdministrator)
@ -141,8 +141,14 @@ namespace Remotely.Server.API
return Unauthorized();
}
if (User.Identity.IsAuthenticated &&
DataService.GetUserByName(User.Identity.Name).Id == userID)
{
return BadRequest("You can't delete yourself here. You must go to the Personal Data page to delete your own account.");
}
Request.Headers.TryGetValue("OrganizationID", out var orgID);
DataService.RemoveUserFromOrganization(orgID, userID);
await DataService.RemoveUserFromOrganization(orgID, userID);
return Ok("ok");
}

View File

@ -79,18 +79,19 @@
<tbody>
@for (var i = 0; i < Model.Users.Count; i++)
{
<tr user="@Model.Users[i].ID">
<td class="middle-aligned"><label class="control-label">@Model.Users[i].UserName</label></td>
@if (currentUser.Id == Model.Users[i].ID)
{
<td>@Html.CheckBoxFor(x => x.Users[i].IsAdmin, new { user = Model.Users[i].ID, @class = "user-is-admin-checkbox", disabled = "disabled" })</td>
}
else
{
<td>@Html.CheckBoxFor(x => x.Users[i].IsAdmin, new { user = Model.Users[i].ID, @class = "user-is-admin-checkbox" })</td>
}
<td><button type="button" class="btn btn-danger remove-user-button" user="@Model.Users[i].ID">Remove</button></td>
</tr>
<tr user="@Model.Users[i].ID">
<td class="middle-aligned"><label class="control-label">@Model.Users[i].UserName</label></td>
@if (currentUser.Id == Model.Users[i].ID)
{
<td>@Html.CheckBoxFor(x => x.Users[i].IsAdmin, new { user = Model.Users[i].ID, @class = "user-is-admin-checkbox", disabled = "disabled" })</td>
<td><button type="button" class="btn btn-danger delete-user-button" user="@Model.Users[i].ID" disabled>Delete</button></td>
}
else
{
<td>@Html.CheckBoxFor(x => x.Users[i].IsAdmin, new { user = Model.Users[i].ID, @class = "user-is-admin-checkbox" })</td>
<td><button type="button" class="btn btn-danger delete-user-button" user="@Model.Users[i].ID">Delete</button></td>
}
</tr>
}
</tbody>
</table>

View File

@ -13,19 +13,27 @@ namespace Remotely.Server.Pages
{
public class IndexModel : PageModel
{
private DataService DataService { get; }
public IndexModel(DataService dataService)
public IndexModel(DataService dataService, SignInManager<RemotelyUser> signInManager)
{
DataService = dataService;
SignInManager = signInManager;
}
public string DefaultPrompt { get; set; }
public List<SelectListItem> DeviceGroups { get; set; } = new List<SelectListItem>();
private DataService DataService { get; }
private SignInManager<RemotelyUser> SignInManager { get; }
public void OnGet()
public async Task<IActionResult> OnGet()
{
if (User?.Identity?.IsAuthenticated == true)
{
var user = DataService.GetUserByName(User.Identity.Name);
if (user is null)
{
await SignInManager.SignOutAsync();
return RedirectToPage();
}
DefaultPrompt = DataService.GetDefaultPrompt(User.Identity.Name);
var groups = DataService.GetDeviceGroupsForUserName(User.Identity.Name);
if (groups?.Any() == true)
@ -37,6 +45,8 @@ namespace Remotely.Server.Pages
{
DefaultPrompt = DataService.GetDefaultPrompt();
}
return Page();
}
}
}

View File

@ -1,4 +1,4 @@
@inject Remotely.Server.Services.ApplicationConfig AppConfig;
@inject Remotely.Server.Services.ApplicationConfig AppConfig
<!DOCTYPE html>
<html>
<head>

View File

@ -17,16 +17,21 @@ namespace Remotely.Server.Services
{
public class DataService
{
public DataService(ApplicationDbContext context, ApplicationConfig appConfig, IHostEnvironment hostEnvironment)
public DataService(ApplicationDbContext context,
ApplicationConfig appConfig,
IHostEnvironment hostEnvironment,
UserManager<RemotelyUser> userManager)
{
RemotelyContext = context;
AppConfig = appConfig;
HostEnvironment = hostEnvironment;
UserManager = userManager;
}
private ApplicationConfig AppConfig { get; }
private IHostEnvironment HostEnvironment { get; }
private ApplicationDbContext RemotelyContext { get; }
private UserManager<RemotelyUser> UserManager { get; }
public bool AddDeviceGroup(string orgID, DeviceGroup deviceGroup, out string deviceGroupID, out string errorMessage)
{
@ -407,6 +412,11 @@ namespace Remotely.Server.Services
{
var user = RemotelyContext.Users.FirstOrDefault(x => x.UserName == username);
if (user is null)
{
return null;
}
return RemotelyContext.DeviceGroups.Where(x => x.OrganizationID == user.OrganizationID) ?? Enumerable.Empty<DeviceGroup>();
}
@ -511,15 +521,23 @@ namespace Remotely.Server.Services
RemotelyContext.SaveChanges();
}
public void RemoveUserFromOrganization(string orgID, string targetUserID)
public async Task RemoveUserFromOrganization(string orgID, string targetUserID)
{
var target = RemotelyContext.Users.FirstOrDefault(x =>
x.Id == targetUserID &&
x.OrganizationID == orgID);
var newOrganization = new Organization();
target.Organization = newOrganization;
RemotelyContext.Organizations.Add(newOrganization);
if (GetOrganizationCount() >= AppConfig.MaxOrganizationCount)
{
await UserManager.DeleteAsync(target);
}
else
{
var newOrganization = new Organization();
target.Organization = newOrganization;
RemotelyContext.Organizations.Add(newOrganization);
}
RemotelyContext.SaveChanges();
}

View File

@ -122,9 +122,9 @@ document.querySelectorAll(".user-is-admin-checkbox").forEach((checkbox) => {
xhr.send(JSON.stringify(ev.currentTarget.checked));
});
});
document.querySelectorAll(".remove-user-button").forEach((removeButton) => {
document.querySelectorAll(".delete-user-button").forEach((removeButton) => {
removeButton.addEventListener("click", (ev) => {
var result = confirm("Are you sure you want to remove this user from the organization?");
var result = confirm("Are you sure you want to delete this user?");
if (result) {
var userID = removeButton.getAttribute("user");
var xhr = new XMLHttpRequest();
@ -142,7 +142,7 @@ document.querySelectorAll(".remove-user-button").forEach((removeButton) => {
xhr.onerror = () => {
showError(xhr);
};
xhr.open("delete", `${location.origin}/api/OrganizationManagement/RemoveUserFromOrganization/${userID}`);
xhr.open("delete", `${location.origin}/api/OrganizationManagement/DeleteUser/${userID}`);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send();
}

File diff suppressed because one or more lines are too long

View File

@ -133,9 +133,9 @@ document.querySelectorAll(".user-is-admin-checkbox").forEach((checkbox: HTMLInpu
xhr.send(JSON.stringify((ev.currentTarget as HTMLInputElement).checked));
})
});
document.querySelectorAll(".remove-user-button").forEach((removeButton: HTMLButtonElement) => {
document.querySelectorAll(".delete-user-button").forEach((removeButton: HTMLButtonElement) => {
removeButton.addEventListener("click", (ev) => {
var result = confirm("Are you sure you want to remove this user from the organization?");
var result = confirm("Are you sure you want to delete this user?");
if (result) {
var userID = removeButton.getAttribute("user");
var xhr = new XMLHttpRequest();
@ -153,7 +153,7 @@ document.querySelectorAll(".remove-user-button").forEach((removeButton: HTMLButt
xhr.onerror = () => {
showError(xhr);
}
xhr.open("delete", `${location.origin}/api/OrganizationManagement/RemoveUserFromOrganization/${userID}`);
xhr.open("delete", `${location.origin}/api/OrganizationManagement/DeleteUser/${userID}`);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send();
}