end
# show projects based on current user visibility
- @memberships = @user.memberships.where(Project.visible_condition(User.current)).to_a
+ @memberships = @user.memberships.preload(:roles, :project).where(Project.visible_condition(User.current)).to_a
respond_to do |format|
format.html {
has_many :members, :foreign_key => 'user_id', :dependent => :destroy
has_many :memberships,
- lambda {preload(:project, :roles).
- joins(:project).
+ lambda {joins(:project).
where("#{Project.table_name}.status<>#{Project::STATUS_ARCHIVED}")},
:class_name => 'Member',
:foreign_key => 'user_id'
@membership_by_project_id[project_id]
end
+ def roles
+ @roles ||= Role.joins(members: :project).where(["#{Project.table_name}.status <> ?", Project::STATUS_ARCHIVED]).where(Member.arel_table[:user_id].eq(id)).uniq
+ end
+
# Returns the user's bult-in role
def builtin_role
@builtin_role ||= Role.non_member
return true if admin?
# authorize if user has at least one role that has this permission
- roles = memberships.collect {|m| m.roles}.flatten.uniq
- roles << (self.logged? ? Role.non_member : Role.anonymous)
- roles.any? {|role|
+ rls = self.roles.to_a
+ rls << builtin_role
+ rls.any? {|role|
role.allowed_to?(action) &&
(block_given? ? yield(role, self) : true)
}
end if include_in_api_response?('users') && !@group.builtin?
api.array :memberships do
- @group.memberships.each do |membership|
+ @group.memberships.preload(:roles, :project).each do |membership|
api.membership do
api.id membership.id
api.project :id => membership.project.id, :name => membership.project.name
attrs = {:id => member_role.role.id, :name => member_role.role.name}
attrs.merge!(:inherited => true) if member_role.inherited_from.present?
api.role attrs
- end
+ end
end
end
end if membership.project