public async Task <GetProjectResponseModel> GetProjectAsync([FromBody] GetProjectModel model) { ClaimsPrincipal principal = HttpContext.User; string username = principal.FindFirstValue("username"); ProjectDbModel dbModel = await _projectRepository.GetProject(model.ProjectId); if (!(string.Equals(dbModel.Owner, username) || (dbModel.Collaborators != null && dbModel.Collaborators.Contains(username)))) { return(new GetProjectResponseModel { Success = false, Error = GetProjectResponseModel.ErrorReason.Unauthorised }); } return(new GetProjectResponseModel { Success = true, Project = new ProjectResponseModel { Id = dbModel.Id, Name = dbModel.Name, LastEdit = dbModel.LastEdit, Owner = dbModel.Owner, Collaborators = dbModel.Collaborators } }); }
public async Task <ProjectResponseModel> CreateProject([FromBody] CreateProjectModel model) { ClaimsPrincipal principal = HttpContext.User; string username = principal.FindFirstValue("username"); string id = Guid.NewGuid().ToString(); ProjectDbModel dbModel = new ProjectDbModel { Id = id, Collaborators = new List <string>(), LastEdit = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), Name = model.Name, Owner = username }; await _projectRepository.AddProject(dbModel); return(new ProjectResponseModel { Id = id, Name = model.Name, Collaborators = new List <string>(), LastEdit = dbModel.LastEdit, Owner = username }); }
public async Task <CreateResponseModel> CreateResourceAsync([FromBody] CreateModel model) { ClaimsPrincipal principal = HttpContext.User; string username = principal.FindFirstValue("username"); ProjectDbModel dbModel = await _projectRepository.GetProject(model.ProjectId); if (!(string.Equals(dbModel.Owner, username) || (dbModel.Collaborators != null && dbModel.Collaborators.Contains(username)))) { return(new CreateResponseModel { Success = false }); } string path = Path.GetFullPath(_projectManager.GetFilePath(model.ProjectId, model.Path)); new FileStream(path, FileMode.Create).Dispose(); await _projectManager.OnFileAdded(model.ProjectId, model.Path); return(new CreateResponseModel { Success = true, File = _projectManager.GetFileModel(model.Path) }); }
public async Task <RenameProjectResponseModel> RenameProject([FromBody] RenameProjectModel model) { ClaimsPrincipal principal = HttpContext.User; string username = principal.FindFirstValue("username"); ProjectDbModel dbModel = await _projectRepository.GetProject(model.ProjectId); if (!(string.Equals(dbModel.Owner, username) || (dbModel.Collaborators != null && dbModel.Collaborators.Contains(username)))) { return(new RenameProjectResponseModel { Success = false, Error = RenameProjectResponseModel.ErrorReason.Unauthorised }); } dbModel.Name = model.Name; await _projectRepository.UpdateProject(dbModel); return(new RenameProjectResponseModel { Success = true }); }
private async Task <string> GetProjectIdAsync() { if (!Context.Items.ContainsKey("project")) { throw new InvalidOperationException("No project is open"); } string projectId = Context.Items["project"] as string; ProjectDbModel dbModel = await _projectRepository.GetProject(projectId); string username = Context.User.FindFirstValue("username"); if (!(string.Equals(dbModel.Owner, username) || (dbModel.Collaborators != null && dbModel.Collaborators.Contains(username)))) { throw new InvalidOperationException("Not allowed access to this project"); } return(projectId); }
public async Task OpenProject(string projectId) { if (Context.Items.ContainsKey("project")) { throw new InvalidOperationException("A project is already open"); } ProjectDbModel dbModel = await _projectRepository.GetProject(projectId); string username = Context.User.FindFirstValue("username"); if (!(string.Equals(dbModel.Owner, username) || (dbModel.Collaborators != null && dbModel.Collaborators.Contains(username)))) { throw new InvalidOperationException("Not allowed access to this project"); } Context.Items.Add("project", projectId); await Groups.AddToGroupAsync(Context.ConnectionId, projectId); await Clients.Caller.SendAsync("FileList", _projectManager.GetFileList(projectId)); }
public async Task <UploadResponseModel> UploadResourceAsync([FromBody] UploadModel model) { ClaimsPrincipal principal = HttpContext.User; string username = principal.FindFirstValue("username"); ProjectDbModel dbModel = await _projectRepository.GetProject(model.ProjectId); if (!(string.Equals(dbModel.Owner, username) || (dbModel.Collaborators != null && dbModel.Collaborators.Contains(username)))) { return(new UploadResponseModel { Success = false }); } string path = Path.GetFullPath(_projectManager.GetFilePath(model.ProjectId, model.Path)); // byte[] decodedBytes = Convert.FromBase64String(Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(model.File))); // string decodedTxt = System.Text.Encoding.UTF8.GetString(decodedBytes); var logWriter = new System.IO.StreamWriter(path); logWriter.WriteLine(model.File); logWriter.Dispose(); // using (FileStream fileStream = new FileStream(path, FileMode.Create)) // { // await model.File.CopyToAsync(fileStream); // } await _projectManager.OnFileAdded(model.ProjectId, model.Path); return(new UploadResponseModel { Success = true, File = _projectManager.GetFileModel(model.Path) }); }
public async Task <BuildProjectResponseModel> BuildProject([FromBody] BuildProjectModel model) { ClaimsPrincipal principal = HttpContext.User; string username = principal.FindFirstValue("username"); ProjectDbModel dbModel = await _projectRepository.GetProject(model.ProjectId); if (!(string.Equals(dbModel.Owner, username) || (dbModel.Collaborators != null && dbModel.Collaborators.Contains(username)))) { return(new BuildProjectResponseModel { Success = false }); } (await _projectManager.GetBuildInfoAsync(model.ProjectId)).PerformBuild(); return(new BuildProjectResponseModel { Success = true }); }
public async Task <AddCollaboratorResponseModel> RemoveCollaborator([FromBody] AddCollaboratorModel model) { ClaimsPrincipal principal = HttpContext.User; string username = principal.FindFirstValue("username"); ProjectDbModel dbModel = await _projectRepository.GetProject(model.ProjectId); if (!(string.Equals(dbModel.Owner, username) || (dbModel.Collaborators != null && dbModel.Collaborators.Contains(username)))) { return(new AddCollaboratorResponseModel { Success = false }); } dbModel.Collaborators.Remove(model.Username); await _projectRepository.UpdateProject(dbModel); return(new AddCollaboratorResponseModel { Success = true }); }