mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 23:07:28 +08:00
FIX: Show groups that user is owner of on groups page.
This commit is contained in:
@ -18,12 +18,7 @@ class GroupsController < ApplicationController
|
|||||||
page_size = 30
|
page_size = 30
|
||||||
page = params[:page]&.to_i || 0
|
page = params[:page]&.to_i || 0
|
||||||
|
|
||||||
groups = Group.order(name: :asc).where(visible: true)
|
groups = Group.visible_groups(current_user)
|
||||||
|
|
||||||
if !guardian.is_admin?
|
|
||||||
groups = groups.where(automatic: false)
|
|
||||||
end
|
|
||||||
|
|
||||||
count = groups.count
|
count = groups.count
|
||||||
groups = groups.offset(page * page_size).limit(page_size)
|
groups = groups.offset(page * page_size).limit(page_size)
|
||||||
|
|
||||||
|
@ -63,6 +63,20 @@ class Group < ActiveRecord::Base
|
|||||||
|
|
||||||
validates :alias_level, inclusion: { in: ALIAS_LEVELS.values}
|
validates :alias_level, inclusion: { in: ALIAS_LEVELS.values}
|
||||||
|
|
||||||
|
scope :visible_groups, ->(user) {
|
||||||
|
groups = Group.order(name: :asc).where("groups.id > 0")
|
||||||
|
|
||||||
|
if !user || !user.admin
|
||||||
|
owner_group_ids = GroupUser.where(user: user, owner: true).pluck(:group_id)
|
||||||
|
|
||||||
|
groups = groups.where("
|
||||||
|
(groups.automatic = false AND groups.visible = true) OR groups.id IN (?)
|
||||||
|
", owner_group_ids)
|
||||||
|
end
|
||||||
|
|
||||||
|
groups
|
||||||
|
}
|
||||||
|
|
||||||
scope :mentionable, lambda {|user|
|
scope :mentionable, lambda {|user|
|
||||||
|
|
||||||
levels = [ALIAS_LEVELS[:everyone]]
|
levels = [ALIAS_LEVELS[:everyone]]
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe Group do
|
describe Group do
|
||||||
|
let(:admin) { Fabricate(:admin) }
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
describe '#builtin' do
|
describe '#builtin' do
|
||||||
context "verify enum sequence" do
|
context "verify enum sequence" do
|
||||||
@ -408,4 +410,44 @@ describe Group do
|
|||||||
expect(group.bio_cooked).to include("unicorn.png")
|
expect(group.bio_cooked).to include("unicorn.png")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".visible_groups" do
|
||||||
|
let(:group) { Fabricate(:group, visible: false) }
|
||||||
|
let(:group_2) { Fabricate(:group, visible: true) }
|
||||||
|
let(:admin) { Fabricate(:admin) }
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
group
|
||||||
|
group_2
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when user is an admin' do
|
||||||
|
it 'should return the right groups' do
|
||||||
|
expect(Group.visible_groups(admin).pluck(:id).sort)
|
||||||
|
.to eq([group.id, group_2.id].concat(Group::AUTO_GROUP_IDS.keys - [0]).sort)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when user is owner of a group' do
|
||||||
|
it 'should return the right groups' do
|
||||||
|
group.add_owner(user)
|
||||||
|
|
||||||
|
expect(Group.visible_groups(user).pluck(:id).sort)
|
||||||
|
.to eq([group.id, group_2.id])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when user is not the owner of any group' do
|
||||||
|
it 'should return the right groups' do
|
||||||
|
expect(Group.visible_groups(user).pluck(:id).sort)
|
||||||
|
.to eq([group_2.id])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'user is nil' do
|
||||||
|
it 'should return the right groups' do
|
||||||
|
expect(Group.visible_groups(nil).pluck(:id).sort).to eq([group_2.id])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user