سؤال

I'm writing a plugin on redmine. I took project and user permissions to my plugin to access a button in a page of redmine.

What i want:

Display a button only when the user AND the project have permissions to do it.

Below my code:

_navigation.html.erb

<% if ?????? -%> Here is the codition where display or not the button.
    <div style="float: left; width: auto; padding-right: 1%">  
    <%= button_to_function l(:gerar_build_project), remote_function(:action => 'printar', :controller => 'GerarVersao')%>
    </div>
<% end -%>

init.rb

  permission :view_repository, :gerar_versao_projeto => :exec_client

  project_module :gerar_versao_projeto do
     permission :view_repository, :gerar_versao_projeto => :exec_client
  end

gerar_versao_controller.rb

before_filter :find_project, :authorize, :only => :exec_client

def exec_client
.
.
.
end

private

def find_project
  @project = Project.find(params[:project_id])
end

So anyone knows what I have to put in my if, to display the button only when the User or Project have permission to it?

Thank you.

هل كانت مفيدة؟

المحلول 2

I solved the problem by my myself. The answer is simple. I just followed the proverb "In the life nothing is created everything is copied" :) and copied a existing code from the redmine as listed below:

<% if User.current.allowed_to?(:view_repository, @project) -%>

With this "if" I check if the user is allowed to view my button inside the project. Is important to note that the :view_repository is the permission which i have put.

Questions about the solution, please feel free to discuss.

Best regards and thanks!

نصائح أخرى

You can add a custom method in model to judge if the user is permitted

# model
def permittable?
  he_is_in_some_types_say_exec_client ? true :false
end

Then in view, call this method on current user.

# view
<% if current_user.permittable? %>
   <%= show_something %>
<% end %>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top