3 Commits

Author SHA1 Message Date
MrYummy
4d9d4a4dfc removed unneeded parentheses and text limits 2017-08-06 13:29:03 +02:00
MrYummy
626ba221e0 Removed swap file 2017-07-11 03:38:17 +02:00
MrYummy
0cebefa406 Added permissions to badges 2017-07-10 21:26:15 +02:00
12 changed files with 131 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -0,0 +1,7 @@
class Badgeassociation < ActiveRecord::Base
belongs_to :badge
belongs_to :forum
belongs_to :forumgroup
end

View File

@@ -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)

View File

@@ -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)

View File

@@ -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?

View File

@@ -29,12 +29,28 @@
<td><%= f.label :role_read_id, "Min. read role" %></td>
<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>
<div class="clear"></div>

View File

@@ -16,11 +16,27 @@
<td><%= f.label :role_read_id, "Min. read role" %></td>
<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 %>
<% end %>

View File

@@ -17,10 +17,26 @@
<td><%= f.label :role_read_id, "Min. read role" %></td>
<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>

View File

@@ -17,10 +17,26 @@
<td><%= f.label :role_read_id, "Min. read role" %></td>
<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>

View File

@@ -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