diff options
author | MrYummy <elemental428@gmail.com> | 2017-07-10 21:26:15 +0200 |
---|---|---|
committer | MrYummy <elemental428@gmail.com> | 2017-07-10 21:26:15 +0200 |
commit | 0cebefa406d49d05e069d2e1bac66e7f297478b9 (patch) | |
tree | 0ac77b32f2b7f77648b6fef362dd70a05b934ed3 | |
parent | 2819989b72a38e30192cd57661bde89bc1ebfc7c (diff) |
Added permissions to badges
-rw-r--r-- | app/controllers/forumgroups_controller.rb | 18 | ||||
-rw-r--r-- | app/controllers/forums_controller.rb | 18 | ||||
-rw-r--r-- | app/models/.forum.rb.swp | bin | 0 -> 12288 bytes | |||
-rw-r--r-- | app/models/badge.rb | 1 | ||||
-rw-r--r-- | app/models/badgeassociation.rb | 7 | ||||
-rw-r--r-- | app/models/forum.rb | 8 | ||||
-rw-r--r-- | app/models/forumgroup.rb | 7 | ||||
-rw-r--r-- | app/models/forumthread.rb | 4 | ||||
-rw-r--r-- | app/views/forumgroups/edit.html.erb | 18 | ||||
-rw-r--r-- | app/views/forumgroups/new.html.erb | 18 | ||||
-rw-r--r-- | app/views/forums/edit.html.erb | 16 | ||||
-rw-r--r-- | app/views/forums/new.html.erb | 16 | ||||
-rw-r--r-- | db/migrate/20170710141543_create_badgeassociations.rb | 10 | ||||
-rw-r--r-- | db/schema.rb | 94 |
14 files changed, 187 insertions, 48 deletions
diff --git a/app/controllers/forumgroups_controller.rb b/app/controllers/forumgroups_controller.rb index fe359af..7b166f7 100644 --- a/app/controllers/forumgroups_controller.rb +++ b/app/controllers/forumgroups_controller.rb @@ -19,6 +19,19 @@ class ForumgroupsController < ApplicationController def update if admin? @group = Forumgroup.find(params[:id]) + group_badges = Badgeassociation.where(forumgroup: @group) + ["read-", "write-"].each_with_index do |p,i| + current_badges = group_badges.where(permission: i+1).pluck(:badge_id) + params.select{|k,v| k.start_with? p}.each do |k,v| + name = k.gsub(p, "") + if current_badges.include? (bid = Badge.find_by(name: name).id) + current_badges.delete bid + else + Badgeassociation.create!(badge: Badge.find_by(name: name), forumgroup: @group, permission: i+1) + end + end + current_badges.each {|b| Badgeassociation.find_by(badge_id: b, forumgroup: @group, permission: i+1).delete} + end if @group.update_attributes(group_params) flash[:notice] = "Forum group updated" redirect_to @group @@ -43,6 +56,11 @@ class ForumgroupsController < ApplicationController def create if admin? @group = Forumgroup.new(group_params) + ["read-", "write-"].each_with_index do |p,i| + params.select{|k,v| k.start_with? p}.each do |k,v| + Badgeassociation.create!(badge: Badge.find_by(name: k.gsub(p, "")), forumgroup: @group, permission: i+1) + end + end if @group.save flash[:notice] = "Forum group created." redirect_to @group diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index 206f01f..2dd3f78 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -35,6 +35,19 @@ class ForumsController < ApplicationController def update if admin? + forum_badges = Badgeassociation.where(forum: @forum) + ["read-", "write-"].each_with_index do |p,i| + current_badges = forum_badges.where(permission: i+1).pluck(:badge_id) + params.select{|k,v| k.start_with? p}.each do |k,v| + name = k.gsub(p, "") + if current_badges.include? (bid = Badge.find_by(name: name).id) + current_badges.delete bid + else + Badgeassociation.create!(badge: Badge.find_by(name: name), forum: @forum, permission: i+1) + end + end + current_badges.each {|b| Badgeassociation.find_by(badge_id: b, forum: @forum, permission: i+1).delete} + end if @forum.update_attributes(forum_params) flash[:notice] = "Forum updated" redirect_to @forum @@ -50,6 +63,11 @@ class ForumsController < ApplicationController def create if admin? @forum = Forum.new(forum_params([:forumgroup_id])) + ["read-", "write-"].each_with_index do |p,i| + params.select{|k,v| k.start_with? p}.each do |k,v| + Badgeassociation.create!(badge: Badge.find_by(name: k.gsub(p, "")), forum: @forum, permission: i+1) + end + end if @forum.save flash[:notice] = "Forum created." redirect_to @forum diff --git a/app/models/.forum.rb.swp b/app/models/.forum.rb.swp Binary files differnew file mode 100644 index 0000000..07c5d02 --- /dev/null +++ b/app/models/.forum.rb.swp diff --git a/app/models/badge.rb b/app/models/badge.rb index ee3de34..6a3b310 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -1,6 +1,7 @@ class Badge < ActiveRecord::Base include Comparable has_many :users + has_and_belongs_to_many :forums def self.get (input) if input.is_a?(String) || input.is_a?(Symbol) diff --git a/app/models/badgeassociation.rb b/app/models/badgeassociation.rb new file mode 100644 index 0000000..df5c697 --- /dev/null +++ b/app/models/badgeassociation.rb @@ -0,0 +1,7 @@ +class Badgeassociation < ActiveRecord::Base + + belongs_to :badge + belongs_to :forum + belongs_to :forumgroup + +end diff --git a/app/models/forum.rb b/app/models/forum.rb index a239dbc..f561d20 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -1,6 +1,10 @@ class Forum < ActiveRecord::Base belongs_to :forumgroup has_many :forumthreads + + has_many :badgeassociations + has_many :badges, through: :badgeassociations + belongs_to :role_read, class_name: "Role", foreign_key: "role_read_id" belongs_to :role_write, class_name: "Role", foreign_key: "role_write_id" has_and_belongs_to_many :labels @@ -18,11 +22,11 @@ class Forum < ActiveRecord::Base end def can_read?(user) - group && group.can_read?(user) && (role_read.nil? || (!user.nil? && user.role >= role_read)) + group && group.can_read?(user) && (role_read.nil? || (!user.nil? && user.role >= role_read) || Badgeassociation.find_by(badge: user.badge, forum: self, permission: 1)) end def can_write?(user) - group.can_write?(user) && (role_write.nil? || (!user.nil? && user.role >= role_write)) + group.can_write?(user) && (role_write.nil? || (!user.nil? && user.role >= role_write || Badgeassociation.find_by(badge: user.badge, forum: self, permission: 2))) end def can_view?(user) diff --git a/app/models/forumgroup.rb b/app/models/forumgroup.rb index f9d156c..c946f3b 100644 --- a/app/models/forumgroup.rb +++ b/app/models/forumgroup.rb @@ -4,7 +4,8 @@ class Forumgroup < ActiveRecord::Base belongs_to :role_write, class_name: "Role", foreign_key: "role_write_id" accepts_nested_attributes_for :forums - + has_many :badgeassociations + has_many :badges, through: :badgeassociations validates_presence_of :name, :position validates_length_of :name, in: 2..20 @@ -14,11 +15,11 @@ class Forumgroup < ActiveRecord::Base end def can_read?(user) - role_read.nil? || (!user.nil? && user.role >= role_read) + role_read.nil? || (!user.nil? && user.role >= role_read) || Badgeassociation.find_by(badge: user.badge, forumgroup: self, permission: 1) end def can_write?(user) - !user.nil? && user.confirmed? && (role_write.nil? || user.role >= role_write) + !user.nil? && user.confirmed? && (role_write.nil? || user.role >= role_write) || Badgeassociation.find_by(badge: user.badge, forumgroup: self, permission: 2) end def can_view?(user) diff --git a/app/models/forumthread.rb b/app/models/forumthread.rb index fd8d3c1..f7712b5 100644 --- a/app/models/forumthread.rb +++ b/app/models/forumthread.rb @@ -70,7 +70,7 @@ class Forumthread < ActiveRecord::Base order_phrase = query || [title, content, reply].select(&:present?).join(" ") user_id = user.try(:id).to_i role_value = user.try(:role).to_i - can_read = "COALESCE(forum_role_read.value, 0) <= ? AND COALESCE(forumgroup_role_read.value, 0) <= ?" + can_read = "(COALESCE(forum_role_read.value, 0) <= ? AND COALESCE(forumgroup_role_read.value, 0) <= ?)" # A user can view sticky threads in write-only forums without read permissions. sticky_can_write = "sticky = true AND (COALESCE(forum_role_write.value, 0) <= ? AND COALESCE(forumgroup_role_write.value, 0) <= ?)" match = ["MATCH (title, forumthreads.content) AGAINST (#{Forumthread.sanitize(order_phrase)})", "MATCH (threadreplies.content) AGAINST (#{Forumthread.sanitize(order_phrase)})", "MATCH (title, forumthreads.content) AGAINST (?) OR MATCH (threadreplies.content) AGAINST (?)", "MATCH (title) AGAINST (?)", "MATCH (forumthreads.content) AGAINST (?)", "MATCH (threadreplies.content) AGAINST (?)"] @@ -86,7 +86,7 @@ class Forumthread < ActiveRecord::Base .joins("LEFT JOIN roles as forumgroup_role_read ON forumgroups.role_read_id = forumgroup_role_read.id") .joins("LEFT JOIN roles as forumgroup_role_write ON forumgroups.role_write_id = forumgroup_role_write.id") - threads = threads.where("forumthreads.user_author_id = ? OR (#{can_read}) OR (#{sticky_can_write})", user_id, role_value, role_value, role_value, role_value) + threads = threads.where("forumthreads.user_author_id = ? OR (#{can_read}) OR (#{sticky_can_write}) OR (?)", user_id, role_value, role_value, role_value, role_value, Forum.find(forum).can_read?(user)) if query threads = threads.where("#{match[2]}", query[0..99], query[0..99]) elsif [title, content, reply].any? diff --git a/app/views/forumgroups/edit.html.erb b/app/views/forumgroups/edit.html.erb index bb3bf5a..4a79f90 100644 --- a/app/views/forumgroups/edit.html.erb +++ b/app/views/forumgroups/edit.html.erb @@ -30,11 +30,27 @@ <td><%= f.select :role_read_id, role_selection, include_blank: "None" %></td> </tr> <tr> + <td><b>Badges with read permission</b></td> + <td> + <% Badge.where("name != 'none'").each do |b| %> + <%=b%><%= check_box_tag "read-#{b}", nil, Badgeassociation.find_by(badge: b, forumgroup: @group, permission: 1) %> + <% end %> + </td> + </tr> + <tr> <td><%= f.label :role_write_id, "Min. write role" %></td> <td><%= f.select :role_write_id, role_selection, include_blank: false %></td> </tr> + <tr> + <td><b>Badges with write permission</b></td> + <td> + <% Badge.where("name != 'none'").each do |b| %> + <%=b%><%= check_box_tag "write-#{b}", nil, Badgeassociation.find_by(badge: b, forumgroup: @group, permission: 2) %> + <% end %> + </td> + </tr> </table> <p><%= f.submit "Update group", class: "btn blue left" %></p> <% end %> <p><%= button_to "Delete group", @group, :method => "delete", data: {confirm: "Delete group?\nForums + Threads will not be accessible!"}, class: "btn red right" %></p> -<div class="clear"></div>
\ No newline at end of file +<div class="clear"></div> diff --git a/app/views/forumgroups/new.html.erb b/app/views/forumgroups/new.html.erb index 9802260..4731522 100644 --- a/app/views/forumgroups/new.html.erb +++ b/app/views/forumgroups/new.html.erb @@ -17,10 +17,26 @@ <td><%= f.select :role_read_id, role_selection, include_blank: "None" %></td> </tr> <tr> + <td><b>Badges with read permission</b></td> + <td> + <% Badge.where("name != 'none'").each do |b| %> + <%=b%><%= check_box_tag "read-#{b}" %> + <% end %> + </td> + </tr> + <tr> <td><%= f.label :role_write_id, "Min. write role" %></td> <td><%= f.select :role_write_id, role_selection, include_blank: false %></td> </tr> + <tr> + <td><b>Badges with write permission</b></td> + <td> + <% Badge.where("name != 'none'").each do |b| %> + <%=b%><%= check_box_tag "write-#{b}" %> + <% end %> + </td> + </tr> </table> <p><%= f.submit "Create group", class: "btn blue left" %></p> <div class="clear"></div> -<% end %>
\ No newline at end of file +<% end %> diff --git a/app/views/forums/edit.html.erb b/app/views/forums/edit.html.erb index 571b3b7..d6d9d13 100644 --- a/app/views/forums/edit.html.erb +++ b/app/views/forums/edit.html.erb @@ -18,10 +18,26 @@ <td><%= f.select :role_read_id, role_selection, include_blank: "None" %></td> </tr> <tr> + <td><b>Badges with read permission</b></td> + <td> + <% Badge.where("name != 'none'").each do |b| %> + <%=b%><%= check_box_tag "read-#{b}", nil, Badgeassociation.find_by(badge: b, forum: @forum, permission: 1) %> + <% end %> + </td> + </tr> + <tr> <td><%= f.label :role_write_id, "Min. write role" %></td> <td><%= f.select :role_write_id, role_selection, include_blank: false %></td> </tr> <tr> + <td><b>Badges with write permission</b></td> + <td> + <% Badge.where("name != 'none'").each do |b| %> + <%=b%><%= check_box_tag "write-#{b}", nil, Badgeassociation.find_by(badge: b, forum: @forum, permission: 2) %> + <% end %> + </td> + </tr> + <tr> <td><%= f.label :necro_length, "Necropost warning delay (in days)" %></td> <td><%= f.number_field :necro_length, placeholder: "Warning Delay (leave blank for no warning)" %></td> </tr> diff --git a/app/views/forums/new.html.erb b/app/views/forums/new.html.erb index 836a9b2..9bf308b 100644 --- a/app/views/forums/new.html.erb +++ b/app/views/forums/new.html.erb @@ -18,10 +18,26 @@ <td><%= f.select :role_read_id, role_selection, include_blank: "None" %></td> </tr> <tr> + <td><b>Badges with read permission</b></td> + <td> + <% Badge.where("name != 'none'").each do |b| %> + <%=b%><%= check_box_tag "read-#{b}" %> + <% end %> + </td> + </tr> + <tr> <td><%= f.label :role_write_id, "Min. write role" %></td> <td><%= f.select :role_write_id, role_selection, include_blank: false %></td> </tr> <tr> + <td><b>Badges with write permission</b></td> + <td> + <% Badge.where("name != 'none'").each do |b| %> + <%=b%><%= check_box_tag "write-#{b}" %> + <% end %> + </td> + </tr> + <tr> <td><%= f.label :necro_length, "Necropost warning delay (in days)" %></td> <td><%= f.number_field :necro_length, placeholder: "Warning Delay (leave blank for no warning)" %></td> </tr> diff --git a/db/migrate/20170710141543_create_badgeassociations.rb b/db/migrate/20170710141543_create_badgeassociations.rb new file mode 100644 index 0000000..cbd5ae0 --- /dev/null +++ b/db/migrate/20170710141543_create_badgeassociations.rb @@ -0,0 +1,10 @@ +class CreateBadgeassociations < ActiveRecord::Migration + def change + create_table :badgeassociations do |t| + t.references :badge + t.references :forum + t.references :forumgroup + t.integer :permission #1 = read, 2 = write + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5849cf5..a0a4b6f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,18 +11,24 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170703003647) do +ActiveRecord::Schema.define(version: 20170710141543) do + + create_table "badgeassociations", force: :cascade do |t| + t.integer "badge_id", limit: 4 + t.integer "forum_id", limit: 4 + t.integer "forumgroup_id", limit: 4 + t.integer "permission", limit: 4 + end create_table "badges", force: :cascade do |t| - t.string "name", limit: 191 - t.string "symbol", limit: 191 - t.string "color", limit: 191 - t.integer "value", limit: 4 + t.string "name", limit: 191 + t.string "symbol", limit: 191 + t.string "color", limit: 191 end create_table "blogposts", force: :cascade do |t| - t.string "title", limit: 255 - t.text "content", limit: 16777215 + t.string "title", limit: 191 + t.text "content", limit: 65535 t.integer "user_author_id", limit: 4 t.integer "user_editor_id", limit: 4 t.datetime "created_at" @@ -30,7 +36,7 @@ ActiveRecord::Schema.define(version: 20170703003647) do end create_table "comments", force: :cascade do |t| - t.text "content", limit: 16777215 + t.text "content", limit: 65535 t.integer "user_author_id", limit: 4 t.integer "user_editor_id", limit: 4 t.integer "blogpost_id", limit: 4 @@ -39,14 +45,14 @@ ActiveRecord::Schema.define(version: 20170703003647) do end create_table "forumgroups", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name", limit: 191 t.integer "position", limit: 4 t.integer "role_read_id", limit: 4 t.integer "role_write_id", limit: 4 end create_table "forums", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name", limit: 191 t.integer "position", limit: 4 t.integer "role_read_id", limit: 4 t.integer "role_write_id", limit: 4 @@ -60,10 +66,10 @@ ActiveRecord::Schema.define(version: 20170703003647) do end create_table "forumthreads", force: :cascade do |t| - t.string "title", limit: 255 - t.text "content", limit: 16777215 - t.boolean "sticky", default: false - t.boolean "locked", default: false + t.string "title", limit: 191 + t.text "content", limit: 65535 + t.boolean "sticky", default: false + t.boolean "locked", default: false t.integer "user_author_id", limit: 4 t.integer "user_editor_id", limit: 4 t.integer "forum_id", limit: 4 @@ -73,49 +79,57 @@ ActiveRecord::Schema.define(version: 20170703003647) do end add_index "forumthreads", ["content"], name: "index_forumthreads_on_content", type: :fulltext - add_index "forumthreads", ["title", "content"], name: "forumthreads_title_content", type: :fulltext add_index "forumthreads", ["title", "content"], name: "index_forumthreads_on_title_and_content", type: :fulltext add_index "forumthreads", ["title"], name: "index_forumthreads_on_title", type: :fulltext create_table "info", force: :cascade do |t| - t.string "title", limit: 255 - t.text "content", limit: 16777215 + t.string "title", limit: 191 + t.text "content", limit: 65535 t.datetime "created_at" t.datetime "updated_at" end create_table "labels", force: :cascade do |t| - t.string "name", limit: 255 - t.string "color", limit: 255 + t.string "name", limit: 191 + t.string "color", limit: 191 + end + + create_table "notifications", force: :cascade do |t| + t.string "user_token", limit: 191 + t.string "message_type", limit: 191, null: false + t.integer "user_message_id", limit: 4 + t.text "content", limit: 65535 + t.string "content_path", limit: 191 + t.datetime "created_at" + t.datetime "updated_at" end create_table "register_tokens", force: :cascade do |t| t.string "uuid", limit: 32, null: false t.string "token", limit: 6, null: false - t.string "email", limit: 191 + t.string "email", limit: 191, null: false end - add_index "register_tokens", ["email"], name: "index_register_tokens_on_email", unique: true, using: :btree add_index "register_tokens", ["uuid"], name: "index_register_tokens_on_uuid", unique: true, using: :btree create_table "roles", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name", limit: 191 t.integer "value", limit: 4 - t.string "color", limit: 255 + t.string "color", limit: 191 end create_table "sessions", force: :cascade do |t| - t.string "session_id", limit: 255, null: false - t.text "data", limit: 16777215 + t.string "session_id", limit: 191, null: false + t.text "data", limit: 65535 t.datetime "created_at" t.datetime "updated_at" end - add_index "sessions", ["session_id"], name: "index_sessions_on_session_id", length: {"session_id"=>191}, using: :btree + add_index "sessions", ["session_id"], name: "index_sessions_on_session_id", using: :btree add_index "sessions", ["updated_at"], name: "index_sessions_on_updated_at", using: :btree create_table "threadreplies", force: :cascade do |t| - t.text "content", limit: 16777215 + t.text "content", limit: 65535 t.integer "user_author_id", limit: 4 t.integer "user_editor_id", limit: 4 t.integer "forumthread_id", limit: 4 @@ -127,19 +141,19 @@ ActiveRecord::Schema.define(version: 20170703003647) do add_index "threadreplies", ["forumthread_id"], name: "index_threadreplies_on_forumthread_id", using: :btree create_table "users", force: :cascade do |t| - t.string "uuid", limit: 255, null: false - t.string "name", limit: 191 - t.string "password_digest", limit: 255, null: false - t.string "ign", limit: 255, null: false - t.string "email", limit: 191 + t.string "uuid", limit: 191, null: false + t.string "name", limit: 191, null: false + t.string "password_digest", limit: 191, null: false + t.string "ign", limit: 191, null: false + t.string "email", limit: 191, null: false t.text "about", limit: 65535 - t.string "last_ip", limit: 255 - t.string "skype", limit: 255 + t.string "last_ip", limit: 191 + t.string "skype", limit: 191 t.boolean "skype_public", default: false - t.string "youtube", limit: 255 - t.string "youtube_channelname", limit: 255 - t.string "twitter", limit: 255 - t.string "email_token", limit: 255 + t.string "youtube", limit: 191 + t.string "youtube_channelname", limit: 191 + t.string "twitter", limit: 191 + t.string "email_token", limit: 191 t.boolean "confirmed", default: false t.datetime "last_seen" t.integer "role_id", limit: 4, null: false @@ -150,10 +164,12 @@ ActiveRecord::Schema.define(version: 20170703003647) do t.boolean "mail_own_blogpost_comment", default: true t.boolean "mail_other_blogpost_comment", default: true t.boolean "mail_mention", default: true - t.integer "badge_id", limit: 4, default: 0 + t.integer "badge_id", limit: 4, default: 1 t.boolean "utc_time", default: false t.boolean "header_scroll", default: false t.boolean "dark", default: false + t.string "notification_token", limit: 191, null: false + t.integer "notification_type", limit: 4, default: 1, null: false end add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree |