Admin Panel: Role editing backend

master
E. Almqvist 3 years ago
parent 37f630f229
commit 7d5e9cb4ef
  1. 54
      src/app.rb
  2. 4
      src/views/admin/index.slim
  3. 8
      src/views/stylesheets/style.sass

@ -47,18 +47,19 @@ not_found do
serve :"404"
end
def auth_denied(msg=AUTH_ERRORS[:denied], status=403)
def auth_denied(msg=AUTH_ERRORS[:denied], status=403, ret="/")
session[:status] = status
session[:ret] = ret
flash[:error] = msg
redirect "/"
end
def no_go_away
auth_denied "No! GO AWAY!"
def no_go_away(ret="/")
auth_denied "No! GO AWAY!", 403, ret
end
def banned
auth_denied "You are banned!"
def banned(ret="/")
auth_denied "You are banned!", 403, ret
end
# Routes
@ -254,17 +255,52 @@ 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"
roleobj = Role.find_by_id id
if roleobj then
serve :"admin/roles/edit", {role: roleobj}
else
raise Sinatra::NotFound
end
end
def verify_flags(flags, userflags)
# TODO: check if this actually works
# should work in practise but who knows
newflags = flags & userflags # only give flags that the user have (max)
flash[:error] = "You are not allowed those flags!" if newflags != flags
return newflags
end
post "/admin/roles/:id/update" do
id = params[:id].to_i
user = get_current_user
auth_denied if user.permitted? :roleman
flags = params[:flags].to_i
flags = verify_flags(flags, user.flags)
data = {
name: params[:name],
color: params[:color],
flags: flags
}
resp = Role.edit id, data
flash[:success] = "Updated role."
redirect "/admin/roles/#{id}/edit"
end
post "/admin/roles" do
auth_denied if get_current_user.permitted? :roleman
user = get_current_user
auth_denied if user.permitted? :roleman
name = params[:name]
color = params[:color]
flags = params[:flags]
flags = params[:flags].to_i
flags = verify_flags(flags, user.flags)
newid, resp = Role.create(name, color, flags)
if newid then
flash[:success] = "Successfully created role '#{name}'."
@ -273,5 +309,3 @@ post "/admin/roles" do
end
redirect "/admin"
end

@ -23,7 +23,7 @@ h1 Admin Panel
li
| #{role.name}
a.inlbutton href="/admin/roles/#{role.id}/edit"
| EDIT
| [EDIT]
a.inlbutton href="/admin/roles/#{role.id}/delete"
| DELETE
| [DELETE]

@ -43,7 +43,7 @@ html
height: 100%
.fade-in
animation: fade-in .4s ease-in alternate
animation: fade-in .5s ease-in alternate
// Layout
body
@ -121,6 +121,9 @@ h1.title
border-bottom: $border_size solid $shadow_clr
.inlbutton
font-weight: bold
text-decoration: none
color: $fg_clr
transition: opacity .2s
.inlbutton:hover
@ -156,6 +159,9 @@ a.button:hover
background: $bg_clr
transition: .4s opacity
div:not(:last-child)
margin-bottom: 1rem
&-error
color: $fg_error
padding: 8px

Loading…
Cancel
Save