Added admin panel
This commit is contained in:
145
Server/app/resources/admin/AdminController.jl
Normal file
145
Server/app/resources/admin/AdminController.jl
Normal file
@@ -0,0 +1,145 @@
|
||||
module AdminController
|
||||
|
||||
using Genie, Genie.Renderer, Genie.Renderer.Html, Genie.Requests, GenieAuthentication, DataFrames, GenieAuthorisation
|
||||
using JSON3
|
||||
using SearchLight,SearchLightPostgreSQL, LibPQ, JSON3
|
||||
using Server.DatabaseSupport, Server.TemplateEditor, Server.Users
|
||||
import Server.DatabaseSupport: select_from_table, insert_into_table, delete_from_table, exist_in_table
|
||||
|
||||
controller = "admin"
|
||||
dict_layouts = Dict(
|
||||
:admin_panel => generate_layout_html("main",controller,"admin-panel"),
|
||||
)
|
||||
|
||||
#---Page info-----------------------------------------------------
|
||||
|
||||
const admin_panel_info = Dict(
|
||||
"en" => Dict(
|
||||
:title => "LibSoc - Admin panel",
|
||||
:description => ""
|
||||
),
|
||||
"ru" => Dict(
|
||||
:title => "",
|
||||
:description => ""
|
||||
)
|
||||
)
|
||||
|
||||
function get_locale()
|
||||
data = payload()
|
||||
if :locale in keys(data)
|
||||
return data[:locale]
|
||||
else
|
||||
return "en"
|
||||
end
|
||||
end
|
||||
|
||||
#---Helpers-----------------------------------------------------------
|
||||
|
||||
|
||||
function table_to_json(name,df)
|
||||
ar = []
|
||||
for df_row in eachrow(df)
|
||||
dict = Dict()
|
||||
for id in names(df_row)
|
||||
dict[id] = df_row[id]
|
||||
end
|
||||
push!(ar,dict)
|
||||
end
|
||||
open("public/assets/"*name*".json", "w") do io
|
||||
JSON3.write(io, ar)
|
||||
end
|
||||
end
|
||||
|
||||
function compile(name)
|
||||
df = select_from_table([name => ["*"]])
|
||||
table_to_json(name,df)
|
||||
end
|
||||
|
||||
function move_requests(name)
|
||||
df_requests = select_from_table(["$(name)_requests" => ["*"]], where_data=["verified" => true, "added" => false])
|
||||
df = select_from_table([name => ["*"]])
|
||||
latitudes = df.latitude
|
||||
longitudes = df.longitude
|
||||
for df_row in eachrow(df_requests)
|
||||
ind_id_given = ismissing(df_row.id_given) ? nothing : findfirst(df_row.id_given.==df.id)
|
||||
if (!isnothing(ind_id_given))
|
||||
id = df[ind_id_given,:id]
|
||||
row_found = df[ind_id_given,Not(:id)]
|
||||
dict = Dict(zip(names(row_found),values(row_found)))
|
||||
dict["members"] += 1
|
||||
update_table(name,dict, where_data=["id" => id])
|
||||
else
|
||||
id = df_row.id
|
||||
dict_update = Dict("added" => true)
|
||||
update_table("$(name)_requests",dict_update, where_data=["id" => id])
|
||||
|
||||
df_row_to_add = df_row[Not(:id_given)]
|
||||
df_row_to_add = df_row_to_add[Not(:verified)]
|
||||
df_row_to_add = df_row_to_add[Not(:added)]
|
||||
df_row_to_add = df_row_to_add[Not(:id)]
|
||||
dict = Dict(zip(names(df_row_to_add),values(df_row_to_add)))
|
||||
dict["members"] = 1
|
||||
insert_into_table(name,dict)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#---Functions---------------------------------------------------------
|
||||
|
||||
current_user() = findone(Users.User, id = get_authentication())
|
||||
|
||||
function admin_panel()
|
||||
@info has_permission(current_user(), "verification")
|
||||
@info current_user()
|
||||
if has_permission(current_user(), "verification")
|
||||
locale = get_locale()
|
||||
html(:admin,:admin_panel, layout = dict_layouts[:admin_panel], context = @__MODULE__,
|
||||
title = admin_panel_info[locale][:title],
|
||||
description = admin_panel_info[locale][:description]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
function verify()
|
||||
if has_permission(current_user(), "verification")
|
||||
data = copy(jsonpayload())
|
||||
user_id = data["user_id"]
|
||||
update_table("users",Dict("verified" => true), where_data=["id" => user_id])
|
||||
return nothing
|
||||
end
|
||||
end
|
||||
|
||||
function get_unverified_users()
|
||||
if has_permission(current_user(), "verification")
|
||||
users = select_from_table("users" => ["id","email"], where_data = ["verified" => false])
|
||||
data = []
|
||||
if size(users,1)!=0
|
||||
for x in eachrow(users)
|
||||
dict = Dict("user_id" => x["id"],"email" => x["email"])
|
||||
push!(data, dict)
|
||||
end
|
||||
end
|
||||
return JSON3.write(data)
|
||||
end
|
||||
end
|
||||
|
||||
function add_verified_groups()
|
||||
if has_permission(current_user(), "admin")
|
||||
groups_create_requests_verified = select_from_table("groups_requests" => ["*"], where_data = ["group_id" => nothing, "status" => 1])
|
||||
if size(groups_create_requests_verified,1)!=0
|
||||
data = Dict(zip(names(groups_create_requests_verified),groups_create_requests_verified[end,:]))
|
||||
user_id = data["user_id"]
|
||||
delete!(data,"group_id")
|
||||
delete!(data,"user_id")
|
||||
delete!(data,"id")
|
||||
delete!(data,"status")
|
||||
group_id = insert_into_table("groups",data, "RETURNING id")[1,1]
|
||||
dict_users_groups = Dict("user_id" => user_id, "group_id" => group_id)
|
||||
insert_into_table("users_groups",dict_users_groups)
|
||||
delete_from_table("groups_requests",["user_id" => user_id])
|
||||
end
|
||||
compile("groups")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
1
Server/app/resources/admin/views/admin_panel.jl.html
Normal file
1
Server/app/resources/admin/views/admin_panel.jl.html
Normal file
@@ -0,0 +1 @@
|
||||
<admin-panel></admin-panel>
|
@@ -245,23 +245,6 @@ function reject_request()
|
||||
return nothing
|
||||
end
|
||||
|
||||
function add_verified_groups()
|
||||
groups_create_requests_verified = select_from_table("groups_requests" => ["*"], where_data = ["group_id" => nothing, "status" => 1])
|
||||
if size(groups_create_requests_verified,1)!=0
|
||||
data = Dict(zip(names(groups_create_requests_verified),groups_create_requests_verified[end,:]))
|
||||
user_id = data["user_id"]
|
||||
delete!(data,"group_id")
|
||||
delete!(data,"user_id")
|
||||
delete!(data,"id")
|
||||
delete!(data,"status")
|
||||
group_id = insert_into_table("groups",data, "RETURNING id")[1,1]
|
||||
dict_users_groups = Dict("user_id" => user_id, "group_id" => group_id)
|
||||
insert_into_table("users_groups",dict_users_groups)
|
||||
delete_from_table("groups_requests",["user_id" => user_id])
|
||||
end
|
||||
compile("groups")
|
||||
end
|
||||
|
||||
function changeMemberCount()
|
||||
user_id = get_authentication()
|
||||
groups_ids = select_from_table("users_groups" => ["group_id"], where_data = ["user_id" => user_id])[:,1]
|
||||
|
140
Server/app/svelte/src/admin-panel.svelte
Normal file
140
Server/app/svelte/src/admin-panel.svelte
Normal file
@@ -0,0 +1,140 @@
|
||||
<svelte:options tag="admin-panel" />
|
||||
|
||||
<script>
|
||||
|
||||
// Import statements
|
||||
import { onMount, getContext } from 'svelte'
|
||||
import { writable } from 'svelte/store'
|
||||
import { getData, sendData } from "/js/libraries/serverTools.js"
|
||||
|
||||
//Import components
|
||||
import "/js/components/select-component.js"
|
||||
import "/js/components/switch-component.js"
|
||||
import "/js/components/pane-aligner.js"
|
||||
|
||||
//Export statements
|
||||
|
||||
// Main code
|
||||
let section
|
||||
let requests_verification = []
|
||||
let loaded = writable(0)
|
||||
let keyRequests = 0
|
||||
let numLoaded = 1
|
||||
let mainPane
|
||||
|
||||
let context = getContext("profile-component")
|
||||
|
||||
function requests_callback(response) {
|
||||
let parsed = JSON.parse(response)
|
||||
requests_verification.push(...parsed)
|
||||
loaded.update((val) => {
|
||||
return val + 1
|
||||
})
|
||||
}
|
||||
getData("/xx/get-unverified-users",requests_callback)
|
||||
|
||||
function approveRequest(ind,user_id) {
|
||||
sendData("/xx/verify",{user_id: user_id})
|
||||
requests_verification.splice(ind,1)
|
||||
keyRequests = keyRequests + 1
|
||||
}
|
||||
|
||||
function addVerified() {
|
||||
getData("/xx/add-verified-groups",() => "")
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
})
|
||||
</script>
|
||||
|
||||
{#key $loaded}
|
||||
{#if $loaded==numLoaded}
|
||||
<pane-aligner>
|
||||
<div bind:this={mainPane} slot="main">
|
||||
<h3>User verification</h3>
|
||||
<section bind:this={section} class="entries-section">
|
||||
{#key keyRequests}
|
||||
{#each requests_verification as req,ind}
|
||||
<div>
|
||||
<div class="change-field-line">
|
||||
<span>{req.email}</span>
|
||||
<div class="request-button-wrapper">
|
||||
<button on:click={() => approveRequest(ind,req.user_id)} class="default-button approve-button">Approve</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
{/key}
|
||||
<button on:click={addVerified} id="add-verified-button" class="default-button">Add verified pins</button>
|
||||
</section>
|
||||
</div>
|
||||
</pane-aligner>
|
||||
{/if}
|
||||
{/key}
|
||||
|
||||
|
||||
<style>
|
||||
@import '/css/common.css';
|
||||
|
||||
.request-button-wrapper {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.default-button {
|
||||
height: 2.7rem;
|
||||
padding: 0rem 1rem;
|
||||
font-family: var(--sans-serif,sans-serif);
|
||||
font-size: 1.15rem;
|
||||
color: white;
|
||||
background-color: var(--red);
|
||||
border-color: var(--red);
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.approve-button {
|
||||
margin-top: -0.45rem;
|
||||
}
|
||||
|
||||
#add-verified-button {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.entries-section {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.entries-section >div {
|
||||
height: 3.5rem;
|
||||
padding-bottom: 0.75rem;
|
||||
padding-top: 0.75rem;
|
||||
border-bottom: 0.14rem solid;
|
||||
border-color: #cdcdcd;
|
||||
}
|
||||
|
||||
/* add padding to every line to center the diving line*/
|
||||
.entries-section >div:last-child {
|
||||
padding-bottom: 0.75rem;
|
||||
padding-top: 0.75rem;
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
/*---Change field-------------------------------------------------------------------*/
|
||||
|
||||
.change-field-line {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
/*---General section-----------------------------------------------------------*/
|
||||
|
||||
h3 {
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
span {
|
||||
font-family: var(--sans-serif,sans-serif);
|
||||
font-size: 1.15rem;
|
||||
}
|
||||
|
||||
|
||||
</style>
|
Reference in New Issue
Block a user