Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
finished default_users that are included in projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Eric Sanderson committed Jan 27, 2010
1 parent 9e88ca1 commit 29dd21b
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 30 deletions.
54 changes: 45 additions & 9 deletions app/controllers/companies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
class CompaniesController < ApplicationController

def edit
unless current_user.admin?
#the functio auto_complete_for_user_name is included in the application controller
before_filter do |controller|
unless controller.current_user.admin?
flash['notice'] = _("Only admins can edit company settings.")
redirect_from_last
return
end
end
before_filter :set_variables

def set_variables
@company = current_user.company
end

def update
@company = current_user.company
def edit
end

def update
@internal = @company.internal_customer

if @internal.nil?
flash['notice'] = 'Unable to find internal customer.'
render :action => 'edit'
return
flash['notice'] = 'Unable to find internal customer.'
render :action => 'edit'
return
end

@company.set_payperiod_date(params[:company][:payperiod_date], current_user.date_format)
Expand All @@ -33,4 +36,37 @@ def update
render :action => 'edit'
end
end

def ajax_remove_permission
debugger
if permission = DefaultUserPermission.find(:first, :conditions => ["user_id = ? AND company_id = ?", params[:user_id], current_user.company_id])
if params[:perm]
permission.remove params[:perm]
permission.save
else
permission.destroy
end
end
render :partial => "permission_list"
end

def ajax_add_permission
user = User.find(params[:user_id], :conditions => ["company_id = ?", current_user.company_id])

if user && DefaultUserPermission.count(:conditions => ["user_id = ? AND company_id = ?", user.id, current_user.company_id]) == 0
permission = DefaultUserPermission.new do |p|
p.user_id = user.id
p.company_id = current_user.company_id
p.can_comment = 1
p.can_work = 1
p.can_close = 1
p.save
end
elsif permission = DefaultUserPermission.find(:first, :conditions => ["user_id = ? AND company_id = ?", params[:user_id], current_user.company_id])
permission.set(params[:perm])
permission.save
end
render :partial => "permission_list"

end
end
19 changes: 19 additions & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def create
@project_permission.can_milestone = 1
@project_permission.can_grant = 1
@project_permission.save

current_user.company.default_user_permissions.each do |pp|
unless @project_permission.user == pp.user
a = pp.attributes
a.delete "created_at"
a.delete "updated_at"
a[:project_id] = @project.id
ProjectPermission.new { |proj| proj.update_attributes a }
end
end

if @project.company.users.size == 1
flash['notice'] = _('Project was successfully created.')
Expand Down Expand Up @@ -111,6 +121,15 @@ def create_shortlist_ajax
@project_permission.can_grant = 1
@project_permission.save

current_user.company.default_user_permissions.each do |pp|
unless @project_permission.user == pp.user
a = pp.attributes
a.delete :created_at
a[:project_id] = @project.id
ProjectPermission.new { |proj| proj.update_attributes a }
end
end

session[:filter_customer_short] = "0"
session[:filter_milestone_short] = "0"
session[:filter_project_short] = @project.id.to_s
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ def auto_complete_for_project_name
end
end

def auto_complete_for_company_name
text = params[:company]
text = text[:name] if text

@companies = []
if !text.blank?
conds = [ "lower(name) like ?", "%#{ text }%" ]
@companies = Company.find(:all, :conditions => conds)
end
end

def project
@user = current_user.company.users.find(params[:id])
project = current_user.company.projects.find(params[:project_id])
Expand Down
3 changes: 2 additions & 1 deletion app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Company < ActiveRecord::Base
has_many :custom_attributes, :dependent => :destroy
has_many :task_filters, :dependent => :destroy
has_many :statuses, :dependent => :destroy, :order => "id asc"
has_many :default_users, :class_name => :user, :through => :default_user_permissions
has_many :default_users, :through => :default_user_permissions, :foreign_key => "user_id", :class_name => "User"
has_many :default_user_permissions

has_many :preferences, :as => :preferencable
include PreferenceMethods
Expand Down
74 changes: 74 additions & 0 deletions app/models/default_user_permission.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class DefaultUserPermission < ActiveRecord::Base
belongs_to :company
belongs_to :default_user, :class_name => "User", :foreign_key => "user_id"
belongs_to :user

def can? (perm)
case perm
when 'comment' then self.can_comment?
when 'work' then self.can_work?
when 'close' then self.can_close?
when 'report' then self.can_report?
when 'create' then self.can_create?
when 'edit' then self.can_edit?
when 'reassign' then self.can_reassign?
when 'prioritize' then self.can_prioritize?
when 'milestone' then self.can_milestone?
when 'grant' then self.can_grant?
when 'all' then (self.can_comment? && self.can_work? && self.can_close? && self.can_report? && self.can_create? && self.can_edit? &&
self.can_reassign? && self.can_prioritize? && self.can_milestone? && self.can_grant?)
end
end

def set(perm)
case perm
when 'comment' then self.can_comment = 1
when 'work' then self.can_work = 1
when 'close' then self.can_close = 1
when 'report' then self.can_report = 1
when 'create' then self.can_create = 1
when 'edit' then self.can_edit = 1
when 'reassign' then self.can_reassign = 1
when 'prioritize' then self.can_prioritize = 1
when 'milestone' then self.can_milestone = 1
when 'grant' then self.can_grant = 1
when 'all' then
self.can_comment = 1
self.can_work = 1
self.can_close = 1
self.can_report = 1
self.can_create = 1
self.can_edit = 1
self.can_reassign = 1
self.can_prioritize = 1
self.can_milestone = 1
self.can_grant = 1
end
end

def remove(perm)
case perm
when 'comment' then self.can_comment = 0
when 'work' then self.can_work = 0
when 'close' then self.can_close = 0
when 'report' then self.can_report = 0
when 'create' then self.can_create = 0
when 'edit' then self.can_edit = 0
when 'reassign' then self.can_reassign = 0
when 'prioritize' then self.can_prioritize = 0
when 'milestone' then self.can_milestone = 0
when 'grant' then self.can_grant = 0
when 'all' then
self.can_comment = 0
self.can_work = 0
self.can_close = 0
self.can_report = 0
self.can_create = 0
self.can_edit = 0
self.can_reassign = 0
self.can_prioritize = 0
self.can_milestone = 0
self.can_grant = 0
end
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class User < ActiveRecord::Base

belongs_to :company
belongs_to :customer
belongs_to :default_user_permission
has_many :projects, :through => :project_permissions, :conditions => ['projects.completed_at IS NULL'], :order => "projects.customer_id, projects.name"
#has_many :projects, :through => :project_permissions, :order => "projects.customer_id, projects.name"
has_many :completed_projects, :through => :project_permissions, :conditions => ['projects.completed_at IS NOT NULL'], :source => :project, :order => "projects.customer_id, projects.name"
Expand Down Expand Up @@ -48,6 +49,7 @@ class User < ActiveRecord::Base
has_many :task_filters, :dependent => :destroy

has_and_belongs_to_many :notice_groups
has_one :default_user_permission, :foreign_key => "user_id"

validates_length_of :name, :maximum=>200, :allow_nil => true
validates_presence_of :name
Expand Down
38 changes: 38 additions & 0 deletions app/views/companies/_permission_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<table class="content" id="user_table" width="100%" style="margin-left:0;margin-right:0;">
<tr>
<th><%=_ 'User' %></th>
<th><%=_ "Remove" %></th>
<th><%=_ 'Comment' %></th>
<th><%=_ 'Work' %></th>
<th><%=_ 'Close' %></th>
<th><%=_ 'Create' %></th>
<th><%=_ 'Edit' %></th>
<th><%=_ 'Assign' %></th>
<th><%=_ 'Prioritize' %></th>
<th><%=_ 'Milestones' %></th>
<th><%=_ 'Reports' %></th>
<th><%=_ 'Grant' %></th>
<th><%=_ 'All' %></th>
</tr>

<% @company.default_users.each do |user| %>
<%= render(:partial => "/users/user_permissions", :locals => {
:user => user, :base => @company, :user_edit => false, :users => @company.default_users, :creator => nil, :perm => user.default_user_permission }) %>
<% end %>

<tr id="add_user">
<td>
<%= _("Add user") %>
</td>
<td colspan="11">
<%= text_field_with_auto_complete(:user, :name, { :value => "" },
:after_update_element => "addUserToCompany",
:select => "complete_value",
:url => "/companies/auto_complete_for_user_name") %>
</td>
</tr>
</table>

<script type="text/javascript" language="javascript" charset="utf-8">
updateTooltips();
</script>
2 changes: 0 additions & 2 deletions app/views/companies/_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@
</label>
<%= incoming_email_select_tag %>
<br />


9 changes: 8 additions & 1 deletion app/views/companies/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<% end %>
</fieldset>

<fieldset>
<legend><%= _("Users added to newly created projects") %></legend>
<div id="permission_list">
<%= render :partial => "permission_list" %>
</div>
</fieldset>

<% @customer = current_user.company.internal_customer %>
<fieldset class="basic">
<legend><%=_ 'Custom Logo' %></legend>
Expand All @@ -22,4 +29,4 @@
<%= submit_tag _("Upload"), :class => 'nolabel' %>
<% end %>
</fieldset>
<%= render :partial => "custom_scripts" %>
<%= render :partial => "custom_scripts" %>
2 changes: 1 addition & 1 deletion app/views/notifications/created_project.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= _ "Project" + " : " + @project.name %>
<%= _ "Client" + " : " + @project.customer.name %>
<%= _ "Time" + " : " + @sent_at.strftime_localized("#{@user.date_format} #{@user.time_format}") %>
<%= _ "Creator" + " : " + @project.owner.username %> (<%= @project.owner.user.name %>)
<%= _ "Creator" + " : " + @project.owner.username %> (<%= @project.owner.username %>)
<%= _ "Users" + " : " %>
<% @project.users.each do |user| %>
- <%= user.username %> (<%= user.name %>)
Expand Down
32 changes: 16 additions & 16 deletions app/views/users/_user_permissions.html.erb
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
<tr class="hoverrow" id="user-<%= base.dom_id %>">
<tr class="hoverrow" id="user-<%= user.dom_id %>">
<td>
<%= h(name) %>
<%= h(user.name) %>
</td>
<%
user_granted = users.include? @user
user_granted = users.include? user
perms = ['comment', 'work', 'close', 'create', 'edit', 'reassign', 'prioritize', 'milestone', 'report', 'grant', 'all']
%>

<% if base.owner and @user == base.owner %>
<% if user == creator %>
<td align="center">
<%= image_tag("user_suit.png", :border => 0, :title => "Can't remove <b>project creator</b>!", :class => "tooltip centered") %>
</td>
<% for p in perms %>
<td align="center">
<%= image_tag("user_suit.png", :border => 0, :title => "Can't remove the <b>project creator</b>!", :class => "tooltip centered") if base_perm.can? p %>
<%= image_tag("delete.png", :border => 0, :title => _("Can't remove the <b>project creator</b>!"), :class => "tooltip centered") unless base_perm.can? p %>
<%= image_tag("user_suit.png", :border => 0, :title => "Can't remove the <b>project creator</b>!", :class => "tooltip centered") if perm and perm.can? p %>
<%= image_tag("delete.png", :border => 0, :title => _("Can't remove the <b>project creator</b>!"), :class => "tooltip centered") unless perm and perm.can? p %>
</td>
<% end %>

<% elsif user_granted %>
<td align="center">
<%= link_to_remote image_tag("tick.png", :border => 0, :title => _("Remove all access for <b>%s</b>?", @user.name), :class => "tooltip centered"),
<%= link_to_remote image_tag("tick.png", :border => 0, :title => _("Remove all access for <b>%s</b>?", user.name), :class => "tooltip centered"),
:update => 'permission_list',
:url => { :controller => 'projects', :action => 'ajax_remove_permission', :user_id => @user.id, :id => base.id, :user_edit => user_edit },
:url => { :controller => controller.controller_name, :action => 'ajax_remove_permission', :user_id => user.id, :id => base.id, :user_edit => user_edit },
:loading => "showProgress();",
:complete => "hideProgress();"
%>
</td>

<% for p in perms %>
<td align="center">
<%= link_to_remote image_tag("tick.png", :border => 0, :title => _("Remove %s access for <b>%s</b>?", p, @user.name), :class => "tooltip centered"),
<%= link_to_remote image_tag("tick.png", :border => 0, :title => _("Remove %s access for <b>%s</b>?", p, user.name), :class => "tooltip centered"),
:update => 'permission_list',
:url => { :controller => 'projects', :action => 'ajax_remove_permission', :user_id => @user.id, :id => base.id, :perm => p, :user_edit => user_edit },
:url => { :controller => controller.controller_name, :action => 'ajax_remove_permission', :user_id => user.id, :id => base.id, :perm => p, :user_edit => user_edit },
:loading => "showProgress();",
:complete => "hideProgress();" if base_perm.can? p %>
<%= link_to_remote image_tag("delete.png", :border => 0, :title => _("Grant %s access for <b>%s</b>?", p, @user.name), :class => "tooltip centered"),
:complete => "hideProgress();" if perm and perm.can? p %>
<%= link_to_remote image_tag("delete.png", :border => 0, :title => _("Grant %s access for <b>%s</b>?", p, user.name), :class => "tooltip centered"),
:update => 'permission_list',
:url => { :controller => 'projects', :action => 'ajax_add_permission', :user_id => @user.id, :id => project.id, :perm => p, :user_edit => user_edit },
:url => { :controller => controller.controller_name, :action => 'ajax_add_permission', :user_id => user.id, :id => base.id, :perm => p, :user_edit => user_edit },
:loading => "showProgress();",
:complete => "hideProgress();" unless perm.can? p %>
:complete => "hideProgress();" unless perm and perm.can? p %>
</td>
<% end %>

<% else %>
<td align="center">
<%= link_to_remote image_tag("delete.png", :border => 0, :title => _("Grant access to <b>%s</b>?", @user.name), :class => "tooltip centered"),
<%= link_to_remote image_tag("delete.png", :border => 0, :title => _("Grant access to <b>%s</b>?", user.name), :class => "tooltip centered"),
:update => 'permission_list',
:url => { :controller => 'projects', :action => 'ajax_add_permission', :user_id => @user.id, :id => project.id, :user_edit => user_edit },
:url => { :controller => controller.controller_name, :action => 'ajax_add_permission', :user_id => user.id, :id => base.id, :user_edit => user_edit },
:loading => "showProgress();",
:complete => "hideProgress();updateDnD();"
%>
Expand Down
Loading

0 comments on commit 29dd21b

Please sign in to comment.