log-time-for-other-users.diff

Gary Watson, 2013-07-08 23:12

Download (5.52 KB)

View differences:

app/controllers/timelog_controller.rb
118 118
  end
119 119

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

  
124 131
    call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
app/helpers/timelog_helper.rb
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)
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
57 73

  
58 74
  def select_hours(data, criteria, value)
59 75
    if value.to_s.empty?
app/models/issue.rb
979 979
        @time_entry = existing_time_entry || TimeEntry.new
980 980
        @time_entry.project = project
981 981
        @time_entry.issue = self
982
        @time_entry.user = User.current
983
        @time_entry.spent_on = User.current.today
984
        @time_entry.attributes = params[:time_entry]
985
        self.time_entries << @time_entry
982
        @time_entry.user ||= User.current # For some unknown reason, if User.current override @time_entry.user when it's already set,
983
        @time_entry.spent_on ||= User.current.today 
984
        @time_entry.attributes = params[:time_entry] # and although this instruction will revert the user to that in params[:time_entry][:user_id]
985
        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.
986 986
      end
987 987

  
988 988
      # TODO: Rename hook
app/models/time_entry.rb
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})"},
......
66 66
  }
67 67

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

  
70 71
  def initialize(attributes=nil, *args)
71 72
    super
app/views/issues/_edit.html.erb
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>
app/views/timelog/_form.html.erb
15 15
  </p>
16 16
  <p><%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %></p>
17 17
  <p><%= f.text_field :hours, :size => 6, :required => true %></p>
18
  <% if User.current.allowed_to?(:edit_time_entries, @project) %>
19
     <p> <%= f.select :user_id, user_collection_for_select_options(@project, @time_entry.user), :required => true %></p>
20
  <% end %>
18 21
  <p><%= f.text_field :comments, :size => 100, :maxlength => 255 %></p>
19 22
  <p><%= f.select :activity_id, activity_collection_for_select_options(@time_entry), :required => true %></p>
20 23
  <% @time_entry.custom_field_values.each do |value| %>