Initial commit
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using WebDesktopBackend.Contract.Persistance;
|
||||
using WebDesktopBackend.Entitys.Files;
|
||||
using WebDesktopBackend.Extentions;
|
||||
using WebDesktopBackend.Options;
|
||||
using FileShare = WebDesktopBackend.Entitys.Files.FileShare;
|
||||
|
||||
namespace WebDesktopBackend.Persistance {
|
||||
public class FileRepository : IFileRepository {
|
||||
private readonly FileSystemOptions _options;
|
||||
private readonly DatabaseContext _context;
|
||||
|
||||
public FileRepository(IOptions<FileSystemOptions> options, DatabaseContext context) {
|
||||
_options = options.Value;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public void InitUser(string userId) {
|
||||
CreateDirectory(_options.RootDirectory, userId);
|
||||
}
|
||||
|
||||
public void DeleteUserFolder(string userId) {
|
||||
Delete(_options.RootDirectory + userId);
|
||||
}
|
||||
|
||||
public bool CreateDirectory(string directory, string name) {
|
||||
DirectoryInfo info = new DirectoryInfo(directory + "/" + name);
|
||||
if (info.Exists) return false;
|
||||
info.Create();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task UploadFile(IFormFile file, string directory) {
|
||||
DirectoryInfo dir = new DirectoryInfo(directory);
|
||||
if (!dir.Exists)
|
||||
dir.Create();
|
||||
|
||||
FileInfo fileInfo = new FileInfo(dir + "/" + file.FileName);
|
||||
if (fileInfo.Exists)
|
||||
fileInfo.Delete();
|
||||
|
||||
FileStream stream = fileInfo.OpenWrite();
|
||||
await file.CopyToAsync(stream);
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public async Task UploadJson(string directory, string name, string data) {
|
||||
DirectoryInfo dir = new DirectoryInfo(directory);
|
||||
if (!dir.Exists)
|
||||
dir.Create();
|
||||
|
||||
FileInfo file = new FileInfo(directory + "/" + name);
|
||||
if (file.Exists)
|
||||
file.Delete();
|
||||
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(data);
|
||||
FileStream stream = file.Create();
|
||||
await stream.WriteAsync(bytes, 0, bytes.Length);
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public FileStream DownloadFile(string path) {
|
||||
FileInfo file = new FileInfo(path);
|
||||
return file.OpenRead();
|
||||
}
|
||||
|
||||
public Task<string> DownloadJson(string file) {
|
||||
return File.ReadAllTextAsync(file);
|
||||
}
|
||||
|
||||
public DirectoryContent GetDirectory(string directory) {
|
||||
DirectoryInfo dir = new DirectoryInfo(directory);
|
||||
|
||||
return new DirectoryContent() {
|
||||
Files = dir.GetFiles().Select(file => file.Name).ToArray(),
|
||||
Directories = dir.GetDirectories().Select(info => info.Name).ToArray()
|
||||
};
|
||||
}
|
||||
|
||||
public DirectoryInformation GetDirectoryInformation(string directory) {
|
||||
DirectoryInfo info = new DirectoryInfo(directory);
|
||||
return new DirectoryInformation {
|
||||
Name = info.Name,
|
||||
Created = Directory.GetCreationTime(directory),
|
||||
Size = info.GetDirectorySize()
|
||||
};
|
||||
}
|
||||
|
||||
public FileInformation GetFileInformation(string file) {
|
||||
FileInfo info = new FileInfo(file);
|
||||
return new FileInformation() {
|
||||
Name = info.Name,
|
||||
Created = File.GetCreationTime(file),
|
||||
Size = info.Length
|
||||
};
|
||||
}
|
||||
|
||||
public void MoveDirectory(string directory, string to) {
|
||||
DirectoryInfo info = new DirectoryInfo(directory);
|
||||
info.MoveTo(to);
|
||||
}
|
||||
|
||||
public void MoveFile(string file, string to) {
|
||||
FileInfo info = new FileInfo(file);
|
||||
info.MoveTo(to);
|
||||
}
|
||||
|
||||
public void Delete(string url) {
|
||||
if (File.Exists(url))
|
||||
File.Delete(url);
|
||||
|
||||
if (Directory.Exists(url))
|
||||
Directory.Delete(url, true);
|
||||
}
|
||||
|
||||
public string GenerateShareId(string url, string owner) {
|
||||
FileShare share = new FileShare();
|
||||
share.File = url;
|
||||
share.Owner = owner;
|
||||
share.Id = Guid.NewGuid().ToString();
|
||||
|
||||
_context.FileShares.Add(share);
|
||||
_context.SaveChanges();
|
||||
|
||||
return share.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using WebDesktopBackend.Contract.Persistance;
|
||||
using WebDesktopBackend.Entitys.Permissions;
|
||||
|
||||
namespace WebDesktopBackend.Persistance {
|
||||
public class GroupRepository : IGroupRepository {
|
||||
private readonly ITokenRepository _tokens;
|
||||
private readonly PermissionGroup[] _groups;
|
||||
|
||||
public GroupRepository(ITokenRepository tokens) {
|
||||
_tokens = tokens;
|
||||
_groups = Program.Groups;
|
||||
}
|
||||
|
||||
public PermissionGroup GetPermissionGroup(string name) {
|
||||
return _groups.SingleOrDefault(group => group.Permission.Equals(name));
|
||||
}
|
||||
|
||||
public PermissionGroup[] GetGroupsFromUser(string userId) {
|
||||
Permission[] permissions = _tokens.GetUserPermissions(userId);
|
||||
return ExtractGroups(permissions);
|
||||
}
|
||||
|
||||
public PermissionGroup[] ExtractGroups(Permission[] permissions) {
|
||||
List<PermissionGroup> permissionGroups = new List<PermissionGroup>();
|
||||
foreach (var permission in permissions) {
|
||||
if (permission.PermissionName.StartsWith("group.")) {
|
||||
foreach (var permissionGroup in _groups) {
|
||||
if (permission.PermissionName.Equals(permissionGroup.Permission)) {
|
||||
permissionGroups.Add(permissionGroup);
|
||||
|
||||
if (permissionGroup.Inherits is not null) {
|
||||
foreach (var inherit in permissionGroup.Inherits) {
|
||||
permissionGroups.Add(GetPermissionGroup(inherit));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return permissionGroups.ToArray();
|
||||
}
|
||||
|
||||
public Permission[] GetUserPermissions(string id) {
|
||||
List<Permission> permissions = _tokens.GetUserPermissions(id)
|
||||
.Where(perm => perm.Type == Permission.Allow).ToList();
|
||||
|
||||
PermissionGroup[] groups = ExtractGroups(permissions.ToArray());
|
||||
foreach (var group in groups) {
|
||||
if (group.Permissions is null) continue;
|
||||
permissions.AddRange(group.Permissions
|
||||
.Select(perm => new Permission {Id = -1, UserId = id, Type = Permission.Allow, PermissionName = perm}));
|
||||
}
|
||||
|
||||
return permissions.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Options;
|
||||
using WebDesktopBackend.Contract.Persistance;
|
||||
using WebDesktopBackend.Entitys.Permissions;
|
||||
using WebDesktopBackend.Entitys.Tokens;
|
||||
using WebDesktopBackend.Security.Authentication;
|
||||
|
||||
namespace WebDesktopBackend.Persistance {
|
||||
public class TokenRepository : ITokenRepository {
|
||||
private readonly JwtTokenAuthenticationOptions _options;
|
||||
private readonly DatabaseContext _context;
|
||||
|
||||
public TokenRepository(IOptions<JwtTokenAuthenticationOptions> options, DatabaseContext context) {
|
||||
_options = options.Value;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public RefreshToken GetRefreshToken(string id) {
|
||||
if (string.IsNullOrEmpty(id)) return null;
|
||||
return _context.RefreshTokens.Where(token => token.Id == id).SingleOrDefault();
|
||||
}
|
||||
|
||||
public AccessToken GetAccessToken(string id) {
|
||||
if (string.IsNullOrEmpty(id)) return null;
|
||||
return _context.AccessTokens.Where(token => token.Id == id).SingleOrDefault();
|
||||
}
|
||||
|
||||
public bool ValidateAccessToken(string id) {
|
||||
AccessToken token = GetAccessToken(id);
|
||||
if (token == null) return false;
|
||||
TimeSpan span = token.ExpirationDate - DateTime.Now;
|
||||
return span.TotalMilliseconds > 0;
|
||||
}
|
||||
|
||||
public bool ValidateRefreshToken(string id) {
|
||||
RefreshToken token = GetRefreshToken(id);
|
||||
if (token == null) return false;
|
||||
TimeSpan span = token.ExpirationDate - DateTime.Now;
|
||||
return span.TotalMilliseconds > 0;
|
||||
}
|
||||
|
||||
public RefreshToken CreateRefreshToken(string userId) {
|
||||
RefreshToken token = new RefreshToken { UserId = userId, Id = Guid.NewGuid().ToString(), ExpirationDate = DateTime.Now.Add(new TimeSpan(int.Parse(_options.RefreshTokenExpirationTimeInHours), 0, 0)) };
|
||||
_context.RefreshTokens.Add(token);
|
||||
_context.SaveChanges();
|
||||
return token;
|
||||
}
|
||||
|
||||
public AccessToken CreateAccessToken(string refreshTokenId) {
|
||||
AccessToken token = new AccessToken { RefreshTokenId = refreshTokenId, Id = Guid.NewGuid().ToString(), ExpirationDate = DateTime.Now.Add(new TimeSpan(0, int.Parse(_options.AccessTokenExpirationTimeInMinutes), 0)) };
|
||||
_context.AccessTokens.Add(token);
|
||||
_context.SaveChanges();
|
||||
return token;
|
||||
}
|
||||
|
||||
public void DeleteUserTokens(string id) {
|
||||
List<RefreshToken> refreshTokens = _context.RefreshTokens.Where(token => token.UserId == id).ToList();
|
||||
refreshTokens.ForEach(token => DeleteRefreshToken(token.Id));
|
||||
}
|
||||
|
||||
public void DeleteRefreshToken(string id) {
|
||||
_context.RefreshTokens.RemoveRange(_context.RefreshTokens.Where(token => token.Id == id));
|
||||
_context.AccessTokens.RemoveRange(_context.AccessTokens.Where(token => token.RefreshTokenId == id));
|
||||
}
|
||||
|
||||
public Permission[] GetUserPermissions(string id) {
|
||||
return _context.Permissions.Where(permission => permission.UserId == id).ToArray();
|
||||
}
|
||||
|
||||
public void AddPermission(string id, string permission) {
|
||||
_context.Permissions.Add(new Permission
|
||||
{ PermissionName = permission, UserId = id, Type = Permission.Allow });
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public void DeletePermission(string id, string permission) {
|
||||
_context.Permissions.Remove(_context.Permissions.Single(perm =>
|
||||
perm.UserId == id && perm.PermissionName == permission));
|
||||
_context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using WebDesktopBackend.Contract.Persistance;
|
||||
using WebDesktopBackend.Entitys.Permissions;
|
||||
using WebDesktopBackend.Entitys.User;
|
||||
|
||||
namespace WebDesktopBackend.Persistance {
|
||||
public class UserRepository : IUserRepository {
|
||||
private readonly DatabaseContext _context;
|
||||
private readonly ITokenRepository _tokens;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public UserRepository(DatabaseContext context, ITokenRepository tokens, IConfiguration configuration) {
|
||||
_context = context;
|
||||
_tokens = tokens;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public User AddUser(UserEditor editor) {
|
||||
User user = new User { Id = Guid.NewGuid().ToString(), Created = DateTime.Now };
|
||||
editor.EditUser(user);
|
||||
user.Password = Hash128(user.Password);
|
||||
_context.Users.Add(user);
|
||||
_context.Permissions.Add(new Permission()
|
||||
{ PermissionName = "group.user", UserId = user.Id, Type = Permission.Allow });
|
||||
_context.SaveChanges();
|
||||
return user;
|
||||
}
|
||||
|
||||
public void EditUser(string id, UserEditor editor) {
|
||||
User user = GetUser(id);
|
||||
if (!string.IsNullOrEmpty(editor.Password))
|
||||
editor.Password = Hash128(editor.Password);
|
||||
editor.EditUser(user);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public void DeleteUser(string id) {
|
||||
_context.Users.RemoveRange(_context.Users.Where(user => user.Id == id));
|
||||
_context.Permissions.RemoveRange(_context.Permissions.Where(permission => permission.UserId == id));
|
||||
_tokens.DeleteUserTokens(id);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public User GetUser(string id) {
|
||||
return _context.Users.SingleOrDefault(user => user.Id == id);
|
||||
}
|
||||
|
||||
public User GetUserByUsername(string username) {
|
||||
return _context.Users.SingleOrDefault(user => user.Username == username);
|
||||
}
|
||||
|
||||
public User GetUserByEmail(string email) {
|
||||
return _context.Users.SingleOrDefault(user => user.Email == email);
|
||||
}
|
||||
|
||||
public User GetUserFromLogin(UserLogin login) {
|
||||
if (!string.IsNullOrEmpty(login.Username)) return GetUserByUsername(login.Username);
|
||||
if (!string.IsNullOrEmpty(login.Email)) return GetUserByEmail(login.Email);
|
||||
return null;
|
||||
}
|
||||
|
||||
public User[] GetUsers() {
|
||||
return _context.Users.OrderBy(user => user.Created).ToArray();
|
||||
}
|
||||
|
||||
public bool Login(UserLogin login) {
|
||||
User user = GetUserFromLogin(login);
|
||||
if (user == null || string.IsNullOrEmpty(user.Password)) return false;
|
||||
return user.Password.Equals(Hash128(login.Password));
|
||||
}
|
||||
|
||||
private string Hash128(string plainText) {
|
||||
try {
|
||||
byte[] salt = _configuration.GetSection("PasswordSalt").Get<byte[]>();
|
||||
|
||||
string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
|
||||
password: plainText,
|
||||
salt: salt,
|
||||
prf: KeyDerivationPrf.HMACSHA256,
|
||||
iterationCount: 100000,
|
||||
numBytesRequested: 256 / 8
|
||||
));
|
||||
|
||||
return hashed;
|
||||
} catch (Exception) { return ""; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user