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" serve :"404"
end end
def auth_denied(msg=AUTH_ERRORS[:denied], status=403) def auth_denied(msg=AUTH_ERRORS[:denied], status=403, ret="/")
session[:status] = status session[:status] = status
session[:ret] = ret
flash[:error] = msg flash[:error] = msg
redirect "/" redirect "/"
end end
def no_go_away def no_go_away(ret="/")
auth_denied "No! GO AWAY!" auth_denied "No! GO AWAY!", 403, ret
end end
def banned def banned(ret="/")
auth_denied "You are banned!" auth_denied "You are banned!", 403, ret
end end
# Routes # Routes
@ -254,17 +255,52 @@ get "/admin/roles/:id/edit" do
id = params[:id].to_i id = params[:id].to_i
role_check id role_check id
flash[:success] = "Much edit. YES" # TODO: make edit stuff roleobj = Role.find_by_id id
redirect "/admin" 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 end
post "/admin/roles" do 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] name = params[:name]
color = params[:color] color = params[:color]
flags = params[:flags] flags = params[:flags]
flags = params[:flags].to_i
flags = verify_flags(flags, user.flags)
newid, resp = Role.create(name, color, flags) newid, resp = Role.create(name, color, flags)
if newid then if newid then
flash[:success] = "Successfully created role '#{name}'." flash[:success] = "Successfully created role '#{name}'."
@ -273,5 +309,3 @@ post "/admin/roles" do
end end
redirect "/admin" redirect "/admin"
end end

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

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

Loading…
Cancel
Save