log-time-for-other-users.diff

Allow users with permission for edit time entries to log time for other users. - Bishma Stornelli Ortega, 2013-03-13 18:36

Download (5.71 KB)

View differences:

app/helpers/timelog_helper.rb (copia de trabajo)
54 54
    activities.each { |a| collection << [a.name, a.id] }
55 55
    collection
56 56
  end
57
  
58
  # Returns a collection of users for a select field.
59
  def user_collection_for_select_options(project, selected = nil)
60
    collection =  project.members.map{|member| member.user }
61
    collection.keep_if{|user| user.allowed_to?(:log_time, project)}    
62
    
63
    s = ''
64
    s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id) if User.current.admin? || collection.include?(User.current)
57 65

  
66
    collection.sort.each do |element|
67
      selected_attribute = ' selected="selected"' if option_value_selected?(element, selected)
68
      s << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
69
    end
70
    
71
    s.html_safe
72
  end
73

  
58 74
  def select_hours(data, criteria, value)
59 75
  	if value.to_s.empty?
60 76
  		data.select {|row| row[criteria].blank? }
app/models/time_entry.rb (copia de trabajo)
24 24
  belongs_to :user
25 25
  belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
26 26

  
27
  attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27
  attr_protected :project_id, :tyear, :tmonth, :tweek
28 28

  
29 29
  acts_as_customizable
30 30
  acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
......
67 67
  }
68 68

  
69 69
  safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
70
  safe_attributes 'user_id', :if => lambda{ |time_entry, user| user.allowed_to?(:edit_time_entries, time_entry.project) }
70 71

  
71 72
  def initialize(attributes=nil, *args)
72 73
    super
app/models/issue.rb (copia de trabajo)
954 954
        @time_entry = existing_time_entry || TimeEntry.new
955 955
        @time_entry.project = project
956 956
        @time_entry.issue = self
957
        @time_entry.user = User.current
958
        @time_entry.spent_on = User.current.today
959
        @time_entry.attributes = params[:time_entry]
960
        self.time_entries << @time_entry
957
        @time_entry.user ||= User.current # For some unknown reason, if User.current override @time_entry.user when it's already set,
958
        @time_entry.spent_on ||= User.current.today 
959
        @time_entry.attributes = params[:time_entry] # and although this instruction will revert the user to that in params[:time_entry][:user_id]
960
        self.time_entries << @time_entry # This instruction will force @time_entry.user to be User.current, independently of what it has already set. Or at least that happened to me.
961 961
      end
962 962

  
963 963
      # TODO: Rename hook
app/controllers/timelog_controller.rb (copia de trabajo)
123 123
  end
124 124

  
125 125
  def create
126
    @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
126
    # Check if current user has permissions to edit time entries
127
    if request.post? and User.current.allowed_to?(:edit_time_entries, @project)
128
      user = User.find(params[:time_entry][:user_id])
129
    else
130
      user = User.current
131
    end
132
    
133
    @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => user, :spent_on => user.today)
127 134
    @time_entry.safe_attributes = params[:time_entry]
128 135

  
129 136
    call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
app/views/timelog/_form.html.erb (copia de trabajo)
12 12
	<p><%= f.text_field :issue_id, :size => 6 %> <em><%= h("#{@time_entry.issue.tracker.name} ##{@time_entry.issue.id}: #{@time_entry.issue.subject}") if @time_entry.issue %></em></p>
13 13
	<p><%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %></p>
14 14
	<p><%= f.text_field :hours, :size => 6, :required => true %></p>
15
	<% if User.current.allowed_to?(:edit_time_entries, @project) %>
16
	<p> <%= f.select :user_id, user_collection_for_select_options(@project, @time_entry.user), :required => true %></p>
17
	<% end %>
15 18
	<p><%= f.text_field :comments, :size => 100 %></p>
16 19
	<p><%= f.select :activity_id, activity_collection_for_select_options(@time_entry), :required => true %></p>
17 20
	<% @time_entry.custom_field_values.each do |value| %>
app/views/issues/_edit.html.erb (copia de trabajo)
18 18
        <div class="splitcontentright">
19 19
        <p><%= time_entry.select :activity_id, activity_collection_for_select_options %></p>
20 20
        </div>
21
        <% if User.current.allowed_to?(:edit_time_entries, @project) %>
22
		<p> <%= time_entry.select :user_id, user_collection_for_select_options(@project, @time_entry.user), :required => true %></p>
23
		<% end %>
21 24
        <p><%= time_entry.text_field :comments, :size => 60 %></p>
22 25
        <% @time_entry.custom_field_values.each do |value| %>
23 26
          <p><%= custom_field_tag_with_label :time_entry, value %></p>