Roles: Role table & relation to user model

master
E. Almqvist 3 years ago
parent 521dfe4b32
commit 1cc4f5fdbd
  1. 4
      src/db_init.rb
  2. 29
      src/db_models.rb
  3. 4
      src/sql/tables/User_Role_relation.sql
  4. 1
      src/views/stylesheets/style.sass
  5. 23
      src/views/user/profile.slim

@ -1,7 +1,9 @@
require_relative "db_models.rb" require_relative "db_models.rb"
LOAD_MODELS = [ LOAD_MODELS = [
User User,
Role,
User_Role_relation
] ]
def db_init def db_init

@ -18,7 +18,19 @@ class User < EntityModel
end end
def role def role
"INSERT ROLE HERE" roles = self.roles
p "####"
p roles
if roles.length > 0 then
role = roles.max_by { |role| role.flags }
return role.name
else
return ""
end
end
def roles
User_Role_relation.get_user_roles @id
end end
def rep_score def rep_score
@ -115,13 +127,12 @@ end
class Role < EntityModel class Role < EntityModel
attr_reader :name, :color, :perm_level attr_reader :name, :color, :flags
def initialize(role_info) def initialize(role_info)
super role_info super role_info
@name = role_info["name"] @name = role_info["name"]
@color = role_info["color"] @color = role_info["color"]
@perm_level = role_info["perm_level"] @flags = role_info["flags"]
# perm_level follow the UNIX perms system
end end
def self.find_by_id(id) def self.find_by_id(id)
@ -134,3 +145,13 @@ class Role < EntityModel
data && Role.new(data) data && Role.new(data)
end end
end end
class User_Role_relation < EntityModel
def self.get_user_roles(user_id)
roleids = self.get "role_id", "user_id = ?", user_id
roles = roleids.map do |ent|
Role.find_by_id(ent["role_id"].to_i)
end
end
end

@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS "User_Role_relation" (
"user_id" INTEGER NOT NULL,
"role_id" INTEGER NOT NULL
);

@ -249,6 +249,7 @@ ul.button-container
#userinfo #userinfo
margin-bottom: 1.75rem margin-bottom: 1.75rem
text-align: center text-align: center
min-width: 250px
h2 h2
margin: 1rem 0 0 0 margin: 1rem 0 0 0
padding: 0 padding: 0

@ -19,21 +19,14 @@
a.button href="/settings" a.button href="/settings"
li Edit profile li Edit profile
#roles.card - if user.roles.length >= 1
h2 Roles #roles.card
.card-container h2 Roles
ul.button-container .card-container
// insert roles here ul.button-container
li - user.roles.each do |role|
label role li
li label = role.name
label role2
li
label role2
li
label role2
li
label role2
#reputation.card #reputation.card
h2 Reputation h2 Reputation

Loading…
Cancel
Save