diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb index 92210ff..9b2351a 100644 --- a/app/controllers/timelog_controller.rb +++ b/app/controllers/timelog_controller.rb @@ -118,7 +118,14 @@ class TimelogController < ApplicationController end def create - @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) + # Check if current user has permissions to edit time entries + if request.post? and User.current.allowed_to?(:edit_time_entries, @project) + user = User.find(params[:time_entry][:user_id]) + else + user = User.current + end + + @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => user, :spent_on => user.today) @time_entry.safe_attributes = params[:time_entry] call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb index cf5629f..b489502 100644 --- a/app/helpers/timelog_helper.rb +++ b/app/helpers/timelog_helper.rb @@ -54,6 +54,22 @@ module TimelogHelper activities.each { |a| collection << [a.name, a.id] } collection end + + # Returns a collection of users for a select field. + def user_collection_for_select_options(project, selected = nil) + collection = project.members.map{|member| member.user } + collection.keep_if{|user| user.allowed_to?(:log_time, project)} + + s = '' + s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id) if User.current.admin? || collection.include?(User.current) + + collection.sort.each do |element| + selected_attribute = ' selected="selected"' if option_value_selected?(element, selected) + s << %() + end + + s.html_safe + end def select_hours(data, criteria, value) if value.to_s.empty? diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index 76200e0..e397617 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -24,7 +24,7 @@ class TimeEntry < ActiveRecord::Base belongs_to :user belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id' - attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek + attr_protected :project_id, :tyear, :tmonth, :tweek acts_as_customizable acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, @@ -66,6 +66,7 @@ class TimeEntry < ActiveRecord::Base } safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' + safe_attributes 'user_id', :if => lambda{ |time_entry, user| user.allowed_to?(:edit_time_entries, time_entry.project) } def initialize(attributes=nil, *args) super diff --git a/app/views/issues/_edit.html.erb b/app/views/issues/_edit.html.erb index f2052a2..086fcd2 100644 --- a/app/views/issues/_edit.html.erb +++ b/app/views/issues/_edit.html.erb @@ -18,6 +18,9 @@

<%= time_entry.select :activity_id, activity_collection_for_select_options %>

+ <% if User.current.allowed_to?(:edit_time_entries, @project) %> +

<%= time_entry.select :user_id, user_collection_for_select_options(@project, @time_entry.user), :required => true %>

+ <% end %>

<%= time_entry.text_field :comments, :size => 60 %>

<% @time_entry.custom_field_values.each do |value| %>

<%= custom_field_tag_with_label :time_entry, value %>

diff --git a/app/views/timelog/_form.html.erb b/app/views/timelog/_form.html.erb index 3256fea..d0c8cdb 100644 --- a/app/views/timelog/_form.html.erb +++ b/app/views/timelog/_form.html.erb @@ -15,6 +15,9 @@

<%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %>

<%= f.text_field :hours, :size => 6, :required => true %>

+ <% if User.current.allowed_to?(:edit_time_entries, @project) %> +

<%= f.select :user_id, user_collection_for_select_options(@project, @time_entry.user), :required => true %>

+ <% end %>

<%= f.text_field :comments, :size => 100, :maxlength => 255 %>

<%= f.select :activity_id, activity_collection_for_select_options(@time_entry), :required => true %>

<% @time_entry.custom_field_values.each do |value| %>