feat: added post deletion, linked account to post

This commit is contained in:
limited_dev 2023-02-02 18:50:50 +01:00
parent 08e54e7036
commit 184ba3a096
5 changed files with 72 additions and 109 deletions

View file

@ -18,6 +18,7 @@
<input type="password" id="password" @bind="Password" /> <input type="password" id="password" @bind="Password" />
</div> </div>
<a @onclick="login" href="javascript:void(0)">[Login]</a> <a @onclick="login" href="javascript:void(0)">[Login]</a>
<button @onclick="login" >Login</button>
</form> </form>
</div> </div>
@ -46,12 +47,6 @@
navManager.NavigateTo("/", true); navManager.NavigateTo("/", true);
return; return;
} }
await js.InvokeVoidAsync("alert", $"Wrong creds:\n{BCrypt.Net.BCrypt.HashPassword(Password)}"); await js.InvokeVoidAsync("alert", $"Wrong Password");
} }
/*
*
UserData target = (await UsersRepository.getUserByEmailAsync(Email));
*/
} }

View file

@ -1,8 +1,10 @@
@using System.ComponentModel.DataAnnotations @using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository @using ImageBoardServerApp.Data.Repository
@inject NavigationManager navigationManager @inject NavigationManager navigationManager
@inject IWebHostEnvironment env @inject IWebHostEnvironment env
@inject AuthenticationStateProvider authStateProvider
<div> <div>
<span>[</span> <span>[</span>
@ -17,11 +19,11 @@
<div class="centered formContent"> <div class="centered formContent">
<div> <div>
<div class="pd centered marg"> <div class="pd centered marg">
<RadzenTextBox Placeholder="Username (Anonymous)" MaxLength="15" Change=@(args => OnChange(args, "username")) Class="w-100"/> <RadzenTextBox Placeholder="Username (Anonymous)" MaxLength="15" @bind-Value="@postUsername" Class="w-100"/>
</div> </div>
<div class="pd centered marg"> <div class="pd centered marg">
<RadzenTextArea Placeholder="Comment..." @bind-Value="@postContent" Cols="30" Rows="6" Change=@(args => OnChange(args, "Content")) Class="w-100"/> <RadzenTextArea Placeholder="Comment..." @bind-Value="@postContent" Cols="30" Rows="6" Class="w-100"/>
</div> </div>
</div> </div>
</div> </div>
@ -51,29 +53,8 @@
[Required] [Required]
public PostData post { get; set; } public PostData post { get; set; }
string postUsername = "Anonymous"; string postUsername { get; set; } = "Anonymous";
string postContent = ""; string postContent { get; set; } = "";
void OnChange(string value, string name)
{
switch (name)
{
case "username":
postUsername = value;
if (value == "")
{
postUsername = "Anonymous";
}
break;
case "content":
postContent = value;
break;
default:
Console.WriteLine("not found.");
break;
}
Console.WriteLine($"Smth changed!: {value}");
}
private IBrowserFile selectedFile; private IBrowserFile selectedFile;
@ -85,29 +66,15 @@
private async Task onPostClick() private async Task onPostClick()
{ {
var userToCreate = new UserData var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
{ var user = await cauthStateProvder.GetAuthenticationStateAsync();
Email = "dev@limited-dev.de", var usr = user.User;
Password = "$2a$10$C/ZPY5aVGkImLGyIP0SySuQaEYIwnY0J99i/m6tqqf6tMkax89Eku", UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
PermissionInteger = 100, int userID = foundusr.UserID;
TimeBanned = -1,
lastActionTimeStamp = DateTime.Now.Millisecond
};
int userID;
UserData foundusr = await UsersRepository.getUserByEmailAsync(userToCreate.Email);
if (foundusr == null)
{
userID = await UsersRepository.createUserAsync(userToCreate);
}
else
{
userID = foundusr.UserID;
if(foundusr.TimeBanned != -1) if(foundusr.TimeBanned != -1)
return; return;
foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds(); foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds();
await UsersRepository.updateUserAsync(foundusr); await UsersRepository.updateUserAsync(foundusr);
}
bool hasImage = selectedFile != null; bool hasImage = selectedFile != null;

View file

@ -4,11 +4,13 @@
@using System.Net.Mime @using System.Net.Mime
@using System.Reflection @using System.Reflection
@using System.Runtime.CompilerServices @using System.Runtime.CompilerServices
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data @using ImageBoardServerApp.Data
@using ImageBoardServerApp.Data.Repository @using ImageBoardServerApp.Data.Repository
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IWebHostEnvironment env @inject IWebHostEnvironment env
@inject AuthenticationStateProvider authStateProvider
<div> <div>
<span>[</span> <span>[</span>
@ -23,15 +25,15 @@
<div class="centered formContent"> <div class="centered formContent">
<div> <div>
<div class="pd centered marg"> <div class="pd centered marg">
<RadzenTextBox Placeholder="Username (Anonymous)" MaxLength="15" Change=@(args => OnChange(args, "username")) Class="w-100"/> <RadzenTextBox Placeholder="Username (Anonymous)" MaxLength="15" @bind-Value="@postUsername" Class="w-100"/>
</div> </div>
<div class="pd centered marg"> <div class="pd centered marg">
<RadzenTextBox Placeholder="Title" MaxLength="20" Change=@(args => OnChange(args, "title")) Class="w-100"/> <RadzenTextBox Placeholder="Title" MaxLength="20" @bind-Value="@postTitle" Class="w-100"/>
</div> </div>
<div class="pd centered marg"> <div class="pd centered marg">
<RadzenTextArea Placeholder="Content..." @bind-Value="@postContent" Cols="30" Rows="6" Change=@(args => OnChange(args, "Content")) Class="w-100"/> <RadzenTextArea Placeholder="Content..." @bind-Value="@postContent" Cols="30" Rows="6" Class="w-100"/>
</div> </div>
</div> </div>
@ -63,29 +65,10 @@
[Required] [Required]
public BoardData board { get; set; } = new BoardData(); public BoardData board { get; set; } = new BoardData();
string postUsername = "Anonymous"; string postUsername { get; set; } = "Anonymous";
string postTitle = ""; string postTitle { get; set; } = "";
string postContent = ""; string postContent { get; set; } = "";
void OnChange(string value, string name)
{
switch (name)
{
case "title":
postTitle = value;
break;
case "username":
postUsername = value;
break;
case "content":
postContent = value;
break;
default:
Console.WriteLine("not found.");
break;
}
Console.WriteLine($"Smth changed!: {value}");
}
private IBrowserFile selectedFile; private IBrowserFile selectedFile;
@ -98,33 +81,21 @@
private async Task onPostClick() private async Task onPostClick()
{ {
var userToCreate = new UserData
{
Email = "test@mail.org",
Password = "$2a$10$C/ZPY5aVGkImLGyIP0SySuQaEYIwnY0J99i/m6tqqf6tMkax89Eku",
PermissionInteger = 100,
TimeBanned = -1,
lastActionTimeStamp = DateTime.Now.Millisecond
};
int userID; var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
UserData foundusr = await UsersRepository.getUserByEmailAsync(userToCreate.Email); var user = await cauthStateProvder.GetAuthenticationStateAsync();
if (foundusr == null) var usr = user.User;
{ UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
userID = await UsersRepository.createUserAsync(userToCreate); int userID = foundusr.UserID;
}
else
{
userID = foundusr.UserID;
if(foundusr.TimeBanned != -1) if(foundusr.TimeBanned != -1)
return; return;
foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds(); foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds();
await UsersRepository.updateUserAsync(foundusr); await UsersRepository.updateUserAsync(foundusr);
}
//TODO Add check if data is image //TODO Add check if data is image
if (selectedFile == null) if (selectedFile == null || selectedFile.Size >= 512000 * 4)
{ {
return; return;
} }

View file

@ -1,5 +1,8 @@
@using System.ComponentModel.DataAnnotations @using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data @using ImageBoardServerApp.Data
@using ImageBoardServerApp.Data.Repository
@inject AuthenticationStateProvider authStateProvider
<div class="threadHeader"> <div class="threadHeader">
@if (showOpenThread) @if (showOpenThread)
@ -41,14 +44,16 @@
</div> </div>
</div> </div>
<div class="threadFooter"> <div class="threadFooter">
<!--<RadzenButton class="bump" Text="bump"></RadzenButton> --> <span>[</span>
<a @onclick="@deletePost" href="javascript:void(0)">Delete</a>
<span>]</span>
<span>[</span> <span>[</span>
<a class="report" href="/report/@post.Board/@post.PostID" target="_blank">Report</a> <a class="report" href="/report/@post.Board/@post.PostID" target="_blank">Report</a>
<span>]</span> <span>]</span>
@if (showOpenThread) @if (showOpenThread)
{ {
<span>[</span> <span>[</span>
<a class="openThread" href="/@post.Board/thread/@post.PostID">(@post.Comments.Count) Open Thread</a> <a class="openThread" href="/@post.Board/thread/@post.PostID">(@post.Comments.Count) View Thread</a>
<span>]</span> <span>]</span>
} }
else else
@ -70,6 +75,18 @@
return dateTime; return dateTime;
} }
private async Task deletePost()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
if (foundusr.PermissionInteger >= 50 || post.UserID == foundusr.UserID)
{
await PostsRepository.deletePostAsync(post.PostID);
}
}
private bool opened = true; private bool opened = true;
private string toggleText = "-"; private string toggleText = "-";

View file

@ -1,6 +1,6 @@
@inherits LayoutComponentBase @inherits LayoutComponentBase
@using ImageBoardServerApp.Auth @using ImageBoardServerApp.Auth
@inject AuthenticationStateProvider authStateProvder @inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager @inject NavigationManager navManager
<PageTitle>BulletBoard</PageTitle> <PageTitle>BulletBoard</PageTitle>
@ -17,7 +17,7 @@
<a href="/rules">[Rules]</a> <a href="/rules">[Rules]</a>
<AuthorizeView> <AuthorizeView>
<Authorized> <Authorized>
<a @onclick="logout" href="javascript:void(0)">[Logout]</a> <a @onclick="logout" href="javascript:void(0)">[Logout @mail]</a>
</Authorized> </Authorized>
<NotAuthorized> <NotAuthorized>
<a href="/login">[Login]</a> <a href="/login">[Login]</a>
@ -33,9 +33,22 @@
@code @code
{ {
private string mail { get; set; } = "";
protected override async Task OnInitializedAsync()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
if (user.User.Identity.IsAuthenticated)
{
mail = user.User.Identity.Name;
}
}
private async Task logout() private async Task logout()
{ {
var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvder; var customAuthStateProvider = (CustomAuthenticationStateProvider) authStateProvider;
await customAuthStateProvider.UpdateAuthenticationStateAsync(null); await customAuthStateProvider.UpdateAuthenticationStateAsync(null);
navManager.NavigateTo("/", true); navManager.NavigateTo("/", true);
} }