Admin Panel: Role deletion & creation

master
E. Almqvist 3 years ago
parent 1cba969100
commit d448aeadc5
  1. 62
      src/app.rb
  2. 5
      src/config.rb
  3. 8
      src/const.rb
  4. 16
      src/db_models.rb
  5. 12
      src/lib/database.rb
  6. BIN
      src/public/avatars/6.png
  7. 2
      src/sql/tables/Role.sql
  8. 29
      src/views/admin.slim
  9. 2
      src/views/layout.slim
  10. 57
      src/views/stylesheets/style.sass

@ -31,11 +31,15 @@ enable :sessions
db_init
before do
if !is_logged_in && request.path_info.start_with?(*AUTH_ROUTES) then
route_auth_needed = request.path_info.start_with?(*AUTH_ROUTES)
if !is_logged_in && route_auth_needed then
session[:ret] = request.fullpath # TODO: return the user to the previous route
session[:status] = 403
flash[:error] = AUTH_ERRORS[:needed]
redirect "/login"
elsif route_auth_needed && get_current_user.banned?
banned
end
end
@ -43,12 +47,16 @@ not_found do
serve :"404"
end
def auth_denied(msg="You are not permitted to do that!", status=403)
def auth_denied(msg=AUTH_ERRORS[:denied], status=403)
session[:status] = status
flash[:error] = msg
redirect "/"
end
def no_go_away
auth_denied "No! GO AWAY!"
end
def banned
auth_denied "You are banned!"
end
@ -160,7 +168,7 @@ post "/user/update" do
success, msg = get_current_user.update_creds data # update the user creds
if not success then flash[:error] = msg end
flash[:success] = "Updated profile"
flash[:success] = "Profile updated."
redirect "/settings"
end
@ -215,11 +223,55 @@ end
# Admin panel
get "/admin" do
flags = get_current_user.flags
p flags.to_s(2)
user = get_current_user
banned unless !user.banned? # reject the user if banned
auth_denied unless user.flags != 0 # reject the user if he/she has no roles
serve :admin, {flags: flags}
data = {
roles: Role.get_all
}
serve :"admin/index", {flags: flags, data: data}
end
def role_check(id)
no_go_away if ROLE_IDS.include? id
auth_denied if get_current_user.permitted? :roleman
end
get "/admin/roles/:id/delete" do
id = params[:id].to_i
role_check id
Role.delete id
flash[:success] = "Removed role."
redirect "/admin"
end
get "/admin/roles/:id/edit" do
id = params[:id].to_i
role_check id
flash[:success] = "Much edit. YES" # TODO: make edit stuff
redirect "/admin"
end
post "/admin/roles" do
auth_denied if get_current_user.permitted? :roleman
name = params[:name]
color = params[:color]
flags = params[:flags]
newid, resp = Role.create(name, color, flags)
if newid then
flash[:success] = "Successfully created role '#{name}'."
else
flash[:error] = resp
end
redirect "/admin"
end

@ -4,7 +4,8 @@ COINS_PREFIX = "€"
COINS_SUFFIX = ""
AUTH_ERRORS = {
needed: "Authentication is needed to perform that task! Please login!"
needed: "Authentication is needed to perform that task! Please login!",
denied: "You are not permitted to do that!"
}
REGISTER_ERRORS = {
@ -13,7 +14,7 @@ REGISTER_ERRORS = {
pass_len: "Password length must be at least #{MIN_PASSWORD_LEN}",
pass_notequals: "Password mismatch",
name_len: "Name length must be between #{MIN_NAME_LEN} and #{MAX_NAME_LEN}",
name_len: "Name length must be between #{MIN_NAME_LEN} and #{MAX_NAME_LEN} characters!",
name_desc: "May only contain alphabetical characters and must be between #{MIN_NAME_LEN} and #{MAX_NAME_LEN} characters long",
email_dupe: "Email is already in use",

@ -7,10 +7,11 @@ MAX_REP = 100
PERM_LEVELS = {
banned: 2**0, # denies the user everything
admin: 2**1, # admin role (gives all flags)
admin: 2**1, # admin role (gives all flags except "banned")
roleman: 2**2, # allows the user to manage roles
cateman: 2**3, # allows the user to manage categories
rmpost: 2**4 # allows the user to remove other peoples auctions
rmpost: 2**4, # allows the user to remove other peoples auctions
moneyman: 2**5 # allows the user to give/take money from people
}
# Constant roles that will always exist
@ -31,6 +32,9 @@ ROLES = {
}
}
ROLE_IDS = []
ROLES.each {|_, role| ROLE_IDS << role[:id]}
# DB stuff
DB_PATH = "db/main.db"

@ -138,7 +138,9 @@ class User < EntityModel
def flags
flags = 0
self.roles.each do |role|
flags |= role.flags
if role.is_a? Role then
flags |= role.flags
end
end
return flags
end
@ -192,6 +194,8 @@ class Role < EntityModel
end
def self.create(name, color="#ffffff", flags=0)
return false, REGISTER_ERRORS[:name_len] unless name.length.between?(MIN_NAME_LEN, MAX_NAME_LEN)
data = {
name: name,
color: color,
@ -203,6 +207,16 @@ class Role < EntityModel
def self.edit(roleid, data)
self.update data, "id = #{roleid}"
end
def self.get_all_ids
ids = self.get "id"
ids.map! {|k, id| id.to_i}
end
def self.get_all
data = self.get "*"
data && data.map! {|r| Role.new(r)}
end
end

@ -77,11 +77,19 @@ class EntityModel
def self.insert(data) # Inserts new data into the table
entstr, valstr = self.gen_insert_query data.keys
dbbuf, resp = self.equery( "INSERT INTO #{self.name} #{entstr} VALUES #{valstr}", *data.values )
newid = dbbuf.last_insert_row_id
begin
dbbuf, resp = self.equery( "INSERT INTO #{self.name} #{entstr} VALUES #{valstr}", *data.values )
newid = dbbuf.last_insert_row_id
rescue SQLite3::ConstraintException
resp = "Constraint Exception! Duplicate item."
end
return newid, resp
end
def self.delete(id)
self.query "DELETE FROM #{self.name} WHERE id = ?", id
end
def self.set(attr, data, filter="") # slower but more lazy
if self.get(attr, filter).length > 0 then
self.update(data, filter)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 147 KiB

After

Width:  |  Height:  |  Size: 202 KiB

@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS "Role" (
"id" INTEGER NOT NULL UNIQUE,
"name" TEXT NOT NULL,
"name" TEXT NOT NULL UNIQUE,
"color" TEXT NOT NULL DEFAULT '#ffffff',
"flags" INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY("id" AUTOINCREMENT)

@ -1 +1,30 @@
h1 Admin Panel
#admin-panel
article.card.border
h2 Manage Roles
ul.list-container
- data[:roles].each do |role|
li
| #{role.name}
a.inlbutton href="/admin/roles/#{role.id}/edit"
| EDIT
a.inlbutton href="/admin/roles/#{role.id}/delete"
| DELETE
article.card.border
h2 Create Role
.form-container
form action="/admin/roles" method="post"
label Role name
input type="text" name="name" pattern="#{NAME_REGEX_STR}" maxlength="#{MAX_NAME_LEN}" oninput="this.reportValidity()" title="#{REGISTER_ERRORS[:name_desc]}"
label Role color
input type="color" name="color" value="#fa0f01"
label Role flags int
input type="number" name="flags" value="0"
input type="submit" value="Create role"

@ -23,7 +23,7 @@ html lang="en"
- else
li.coins = "#{COINS_PREFIX}#{session_user.balance}#{COINS_SUFFIX}"
- if session_user.admin? && !session_user.banned?
- if session_user.flags != 0 && !session_user.banned?
li
a.inlbutton href="/admin" target="_self"
img.icon src="/icons/shield.svg" alt="Admin Panel"

@ -223,6 +223,17 @@ ul.button-container
a
color: #fff
ul.list-container
display: flex
flex-direction: column
padding: 0
margin: 0
list-style-type: none
height: 15rem
overflow-y: scroll
li
display: flex
.form-container
display: flex
@ -232,7 +243,7 @@ ul.button-container
display: flex
flex-direction: column
justify-content: center
padding: 2rem
padding: 1rem
margin: 0
width: 50rem
max-width: 400px
@ -292,6 +303,22 @@ ul.button-container
.gray
color: $gray_clr
.card-container
padding: 0 1rem
.card
background-color: $bg_clr
border-radius: $border_radius
h2
text-align: left
padding: 0 .8rem
margin: 0
border-bottom: $border_size solid $shadow_clr
font-size: 1rem
color: $fg_clr
.border
border: $border_size solid $shadow_clr
// Profiles
#profile-container
display: flex
@ -302,21 +329,6 @@ ul.button-container
padding-top: 2rem
margin: 0 auto
.card-container
padding: 0 1rem
.card
background-color: $bg_clr
//border: $border_size solid $shadow_clr
border-radius: $border_radius
h2
text-align: left
padding-left: .8rem
margin: 0
border-bottom: $border_size solid $shadow_clr
font-size: 1rem
color: $fg_clr
aside
margin-right: 2rem
#userinfo
@ -397,6 +409,19 @@ ul.button-container
padding: 0 .2rem
border: $border_size solid $border_clr
#admin-panel
display: flex
flex-wrap: wrap
article
margin: 2rem
.list-container
padding: 1rem
li
a
margin-left: 1rem
// Auctions
#auctions
display: flex

Loading…
Cancel
Save