public Models.File Put(int id, [FromBody] Models.File file) { var File = _files.GetFile(file.FileId, false); if (ModelState.IsValid && File != null && File.Folder.SiteId == _alias.SiteId && _userPermissions.IsAuthorized(User, EntityNames.Folder, file.FolderId, PermissionNames.Edit)) { if (File.Name != file.Name || File.FolderId != file.FolderId) { string folderpath = _folders.GetFolderPath(file.Folder); if (!Directory.Exists(folderpath)) { Directory.CreateDirectory(folderpath); } System.IO.File.Move(_files.GetFilePath(File), Path.Combine(folderpath, file.Name)); } file.Extension = Path.GetExtension(file.Name).ToLower().Replace(".", ""); file = _files.UpdateFile(file); _logger.Log(LogLevel.Information, this, LogFunction.Update, "File Updated {File}", file); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized File Put Attempt {File}", file); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; file = null; } return(file); }
public async Task Delete(int id, string siteid) { int SiteId; User user = _users.GetUser(id); if (user != null && int.TryParse(siteid, out SiteId) && SiteId == _alias.SiteId) { // remove user roles for site foreach (UserRole userrole in _userRoles.GetUserRoles(user.UserId, SiteId).ToList()) { _userRoles.DeleteUserRole(userrole.UserRoleId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Role Deleted {UserRole}", userrole); } // remove user folder for site var folder = _folders.GetFolder(SiteId, Utilities.PathCombine("Users", user.UserId.ToString(), Path.DirectorySeparatorChar.ToString())); if (folder != null) { if (Directory.Exists(_folders.GetFolderPath(folder))) { Directory.Delete(_folders.GetFolderPath(folder), true); } _folders.DeleteFolder(folder.FolderId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Folder Deleted {Folder}", folder); } // delete user if they are not a member of any other sites if (!_userRoles.GetUserRoles(user.UserId, -1).Any()) { // get identity user IdentityUser identityuser = await _identityUserManager.FindByNameAsync(user.Username); if (identityuser != null) { // delete identity user var result = await _identityUserManager.DeleteAsync(identityuser); if (result != null) { // delete user _users.DeleteUser(user.UserId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Deleted {UserId}", user.UserId); } else { _logger.Log(LogLevel.Error, this, LogFunction.Delete, "Error Deleting User {UserId}", user.UserId, result.ToString()); } } } } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized User Delete Attempt {UserId}", id); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; } }
public async Task Delete(int id, string siteid) { User user = _users.GetUser(id); if (user != null) { // remove user roles for site foreach (UserRole userrole in _userRoles.GetUserRoles(user.UserId, Int32.Parse(siteid)).ToList()) { _userRoles.DeleteUserRole(userrole.UserRoleId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Role Deleted {UserRole}", userrole); } // remove user folder for site var folder = _folders.GetFolder(Int32.Parse(siteid), Utilities.PathCombine("Users", user.UserId.ToString(), Path.DirectorySeparatorChar.ToString())); if (folder != null) { if (Directory.Exists(_folders.GetFolderPath(folder))) { Directory.Delete(_folders.GetFolderPath(folder), true); } _folders.DeleteFolder(folder.FolderId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Folder Deleted {Folder}", folder); } // delete user if they are not a member of any other sites if (!_userRoles.GetUserRoles(user.UserId, -1).Any()) { // get identity user IdentityUser identityuser = await _identityUserManager.FindByNameAsync(user.Username); if (identityuser != null) { // delete identity user var result = await _identityUserManager.DeleteAsync(identityuser); if (result != null) { // delete user _users.DeleteUser(user.UserId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Deleted {UserId}", user.UserId); } else { _logger.Log(LogLevel.Error, this, LogFunction.Delete, "Error Deleting User {UserId}", user.UserId, result.ToString()); } } } } }
public string GetFilePath(File file) { if (file == null) return null; var folder = file.Folder ?? _db.Folder.Find(file.FolderId); var filepath = Path.Combine(_folderRepository.GetFolderPath(folder), file.Name); return filepath; }