restrict_access_macro.rb

Bernhard Rohloff, 2018-06-21 13:00

Download (827 Bytes)

 
1
Redmine::WikiFormatting::Macros.register do
2
  desc "Restricts content access to specific project role IDs. Example {{restrict_access(1,2,4) Only for DevOps team.}}"
3
  macro :restrict_access do |obj, args, text|
4
    if User.current.logged?
5
      if User.current.admin?
6
        out = h(textilizable(text))
7
      else
8
        membership = User.current.membership(@project)
9

    
10
        permitted_roles = args.to_a
11
        permitted_roles.map!{ |e| e.to_i }
12

    
13
        project_roles = membership.roles.to_a
14
        project_roles.map!{ |e| e.id }
15

    
16
        matching_roles = project_roles & permitted_roles
17

    
18
        if matching_roles.any?
19
          out = h(textilizable(text))
20
        else
21
          out = h("Access prohibited - no member")
22
        end
23
      end
24
    else
25
      out = h("Access prohibited - not logged in")
26
    end
27
  end
28
end