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" />
</div>
<a @onclick="login" href="javascript:void(0)">[Login]</a>
<button @onclick="login" >Login</button>
</form>
</div>
@ -46,12 +47,6 @@
navManager.NavigateTo("/", true);
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 ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository
@inject NavigationManager navigationManager
@inject IWebHostEnvironment env
@inject AuthenticationStateProvider authStateProvider
<div>
<span>[</span>
@ -17,11 +19,11 @@
<div class="centered formContent">
<div>
<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 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>
@ -51,29 +53,8 @@
[Required]
public PostData post { get; set; }
string postUsername = "Anonymous";
string postContent = "";
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}");
}
string postUsername { get; set; } = "Anonymous";
string postContent { get; set; } = "";
private IBrowserFile selectedFile;
@ -85,29 +66,15 @@
private async Task onPostClick()
{
var userToCreate = new UserData
{
Email = "dev@limited-dev.de",
Password = "$2a$10$C/ZPY5aVGkImLGyIP0SySuQaEYIwnY0J99i/m6tqqf6tMkax89Eku",
PermissionInteger = 100,
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)
return;
foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds();
await UsersRepository.updateUserAsync(foundusr);
}
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
int userID = foundusr.UserID;
if(foundusr.TimeBanned != -1)
return;
foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds();
await UsersRepository.updateUserAsync(foundusr);
bool hasImage = selectedFile != null;

View file

@ -4,11 +4,13 @@
@using System.Net.Mime
@using System.Reflection
@using System.Runtime.CompilerServices
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data
@using ImageBoardServerApp.Data.Repository
@inject NavigationManager NavigationManager
@inject IWebHostEnvironment env
@inject AuthenticationStateProvider authStateProvider
<div>
<span>[</span>
@ -23,15 +25,15 @@
<div class="centered formContent">
<div>
<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 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 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>
@ -63,29 +65,10 @@
[Required]
public BoardData board { get; set; } = new BoardData();
string postUsername = "Anonymous";
string postTitle = "";
string postContent = "";
string postUsername { get; set; } = "Anonymous";
string postTitle { get; set; } = "";
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;
@ -98,33 +81,21 @@
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;
UserData foundusr = await UsersRepository.getUserByEmailAsync(userToCreate.Email);
if (foundusr == null)
{
userID = await UsersRepository.createUserAsync(userToCreate);
}
else
{
userID = foundusr.UserID;
if(foundusr.TimeBanned != -1)
return;
foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds();
await UsersRepository.updateUserAsync(foundusr);
}
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
int userID = foundusr.UserID;
if(foundusr.TimeBanned != -1)
return;
foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds();
await UsersRepository.updateUserAsync(foundusr);
//TODO Add check if data is image
if (selectedFile == null)
if (selectedFile == null || selectedFile.Size >= 512000 * 4)
{
return;
}

View file

@ -1,5 +1,8 @@
@using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data
@using ImageBoardServerApp.Data.Repository
@inject AuthenticationStateProvider authStateProvider
<div class="threadHeader">
@if (showOpenThread)
@ -41,14 +44,16 @@
</div>
</div>
<div class="threadFooter">
<!--<RadzenButton class="bump" Text="bump"></RadzenButton> -->
<span>[</span>
<a @onclick="@deletePost" href="javascript:void(0)">Delete</a>
<span>]</span>
<span>[</span>
<a class="report" href="/report/@post.Board/@post.PostID" target="_blank">Report</a>
<span>]</span>
@if (showOpenThread)
{
<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>
}
else
@ -70,6 +75,18 @@
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 string toggleText = "-";

View file

@ -1,6 +1,6 @@
@inherits LayoutComponentBase
@using ImageBoardServerApp.Auth
@inject AuthenticationStateProvider authStateProvder
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager
<PageTitle>BulletBoard</PageTitle>
@ -17,7 +17,7 @@
<a href="/rules">[Rules]</a>
<AuthorizeView>
<Authorized>
<a @onclick="logout" href="javascript:void(0)">[Logout]</a>
<a @onclick="logout" href="javascript:void(0)">[Logout @mail]</a>
</Authorized>
<NotAuthorized>
<a href="/login">[Login]</a>
@ -33,9 +33,22 @@
@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()
{
var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvder;
var customAuthStateProvider = (CustomAuthenticationStateProvider) authStateProvider;
await customAuthStateProvider.UpdateAuthenticationStateAsync(null);
navManager.NavigateTo("/", true);
}