Skip to content

ユーザーロールを追加した #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def set_timezone
end

def load_project
@project = Project.find(params[:project_id])
@project = current_user.projects.find(params[:project_id])
end

def load_stage
Expand Down
19 changes: 14 additions & 5 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ProjectsController < ApplicationController
respond_to :html, :xml, :json

before_filter :load_templates, :only => [:new, :create, :edit, :update]
before_filter :load_user_roles, :only => [:new, :create, :edit, :update]
before_filter :ensure_admin, :only => [:new, :edit, :destroy, :create, :update]

# GET /projects/dashboard
Expand All @@ -12,13 +13,13 @@ def dashboard

# GET /projects
def index
@projects = Project.find(:all, :order => 'name ASC')
@projects = current_user.projects.find(:all, :order => 'name ASC')
respond_with(@projects)
end

# GET /projects/1
def show
@project = Project.find(params[:id])
@project = current_user.projects.find(params[:id])
respond_with(@project)
end

Expand All @@ -37,14 +38,18 @@ def new

# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
@project = current_user.projects.find(params[:id])
respond_with @project
end

# POST /projects
def create
user_role_project = UserRoleProject.new
user_role_project.user_role_id = params[:user_role]

@project = Project.unscoped.where(params[:project]).first_or_create
@project.clone(@original) if load_clone_original
@project.user_role_projects << user_role_project
@project.save

flash[:notice] = 'Project was successfully created.'
Expand All @@ -53,7 +58,7 @@ def create

# PUT /projects/1
def update
@project = Project.find(params[:id])
@project = current_user.projects.find(params[:id])

if @project.update_attributes(params[:project])
flash[:notice] = 'Project was successfully updated.'
Expand All @@ -65,14 +70,18 @@ def update

# DELETE /projects/1
def destroy
@project = Project.find(params[:id])
@project = current_user.projects.find(params[:id])
@project.destroy

redirect_to projects_path, :notice => 'Project was successfully deleted.'
end

private

def load_user_roles
@user_roles = current_user.user_roles.map {|user_role| [user_role.name, user_role.id] }
end

def load_templates
@templates = ProjectConfiguration.templates.sort.collect do |key, val|
[key.to_s.titleize, key.to_s]
Expand Down
36 changes: 36 additions & 0 deletions app/controllers/user_role_projects_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class UserRoleProjectsController < ApplicationController
respond_to :html
before_filter :ensure_admin, only: [:new, :destroy, :create]

def new
@user_role = UserRole.find(params[:user_role_id])
@user_role_project = @user_role.user_role_projects.new
set_projects

respond_with(@user_role_project)
end

def create
@user_role_project = UserRoleProject.where(params[:user_role_project]).first_or_create

if @user_role_project
flash[:notice] = 'UserRoleProject was successfully created.'
respond_with(@user_role_project.user_role, location: @user_role_project.user_role)
else
set_projects
respond_with(@user_role_project)
end
end

def destroy
@user_role_project = UserRoleProject.find(params[:id])
@user_role_project.destroy

redirect_to user_role_path(@user_role_project.user_role_id), notice: 'UserRoleProject was successfully deleted.'
end

private
def set_projects
@projects = Project.scoped.map {|project| [project.name, project.id] }
end
end
36 changes: 36 additions & 0 deletions app/controllers/user_role_users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class UserRoleUsersController < ApplicationController
respond_to :html
before_filter :ensure_admin, only: [:new, :destroy, :create]

def new
@user_role = UserRole.find(params[:user_role_id])
@user_role_user = @user_role.user_role_users.new

set_users
respond_with(@user_role_user)
end

def create
@user_role_user = UserRoleUser.where(params[:user_role_user]).first_or_create

if @user_role_user
flash[:notice] = 'UserRoleUser was successfully created.'
respond_with(@user_role_user.user_role, location: @user_role_user.user_role)
else
set_users
respond_with(@user_role_user)
end
end

def destroy
@user_role_user = UserRoleUser.find(params[:id])
@user_role_user.destroy

redirect_to user_role_path(@user_role_user.user_role_id), notice: 'UserRoleUser was successfully deleted.'
end

private
def set_users
@users = User.all.map {|user| [user.login, user.id] }
end
end
53 changes: 53 additions & 0 deletions app/controllers/user_roles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class UserRolesController < ApplicationController
respond_to :html, :xml, :json
before_filter :ensure_admin, only: [:new, :edit, :destroy, :create, :update]

def index
@user_roles = UserRole.order(:name)
respond_with(@user_roles)
end

def show
@user_role = UserRole.find(params[:id])
respond_with(@user_role)
end

def new
@user_role = UserRole.new
respond_with(@user_role)
end

def edit
@user_role = UserRole.find(params[:id])
respond_with(@user_role)
end

def create
@user_role = UserRole.unscoped.where(params[:user_role]).first_or_create

if @user_role
flash[:notice] = 'UserRole was successfully created.'
respond_with(@user_role, location: @user_role)
else
respond_with(@user_role)
end
end

def update
@user_role = UserRole.find(params[:id])

if @user_role.update_attributes(params[:user_role])
flash[:notice] = 'UserRole was successfully updated.'
respond_with(@user_role, location: @user_role)
else
respond_with(@user_role)
end
end

def destroy
@user_role = UserRole.find(params[:id])
@user_role.destroy

redirect_to user_roles_path, notice: 'UserRole was successfully deleted.'
end
end
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def active_link_class(item)
found = true if @user && @user == item
when 'Stage'
found = true if @stage && @stage == item
when 'UserRole'
found = true if @user_role && @user_role == item
end

if found
Expand Down
4 changes: 3 additions & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ class Project < ActiveRecord::Base
has_many :stages, :dependent => :destroy, :order => 'name ASC'
has_many :deployments, :through => :stages
has_many :configuration_parameters, :dependent => :destroy, :class_name => "ProjectConfiguration", :order => 'name ASC'

has_many :user_role_projects, dependent: :destroy
has_many :user_roles, through: :user_role_projects

validates :name, :uniqueness => true, :presence => true, :length => {:maximum => 250}
validates :template, :inclusion => {:in => ProjectConfiguration.templates.keys}

Expand Down
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class User < ActiveRecord::Base
attr_accessible :login, :email, :password, :password_confirmation, :remember_me, :time_zone, :tz, :admin

has_many :deployments, :dependent => :nullify, :order => 'created_at DESC'
has_many :user_role_users, dependent: :destroy
has_many :user_roles, through: :user_role_users

validates :login, :presence => true, :uniqueness => {:case_sensitive => false}, :length => {:within => 3..40}
validate :guard_last_admin, :on => :update
Expand Down Expand Up @@ -45,6 +47,11 @@ def enable!
self.update_column(:disabled_at, nil)
end

def projects
project_ids = self.user_roles.joins(:user_role_projects).pluck('user_role_projects.project_id')
Project.where(id: project_ids)
end

private

def guard_last_admin
Expand Down
6 changes: 6 additions & 0 deletions app/models/user_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UserRole < ActiveRecord::Base
has_many :user_role_users, dependent: :destroy
has_many :users, through: :user_role_users
has_many :user_role_projects, dependent: :destroy
has_many :projects, through: :user_role_projects
end
4 changes: 4 additions & 0 deletions app/models/user_role_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserRoleProject < ActiveRecord::Base
belongs_to :user_role
belongs_to :project
end
4 changes: 4 additions & 0 deletions app/models/user_role_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserRoleUser < ActiveRecord::Base
belongs_to :user_role
belongs_to :user
end
6 changes: 6 additions & 0 deletions app/views/layouts/_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@
:path => users_path
})%>

<%= render(:partial => 'layouts/menu_box',
:locals => {
:box_type => 'user_roles',
:status => controller_in_use_or(UserRolesController, :open, UserRole),
:path => user_roles_path
})%>
6 changes: 3 additions & 3 deletions app/views/layouts/_projects.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div id="projects_open_content" style="<%= show_if_opened(status) %>">
<% if Project.count > 0 %>
<% for project in Project.find(:all, :order => "name ASC") %>
<% if current_user.projects.count > 0 %>
<% for project in current_user.projects.order(:name) %>
<a href="<%= project_path(project) %>" class="menu_link <%= active_link_class(project) %>" title="<%= project.name %>">
<div class="menu_icons">
<%= image_tag('peritor_theme/arrow_menu_right.gif', :width => '20', :height => '20', :border => '0', :style => "#{do_not_show_stages_of_project(project)}", :id => "#{dom_id(project)}_arrow_right", :onclick => "open_menu('#{dom_id(project)}'); return false;" )%>
Expand Down Expand Up @@ -36,7 +36,7 @@
<div id="projects_closed_content" style="<%= show_if_closed(status) %>">
<a href="javascript:App.open_menu_box('projects')" class="menu_link menu_link_create">
<div class="menu_link_title">
<%= pluralize(Project.count, 'project')%>
<%= pluralize(current_user.projects.count, 'project')%>
</div>
</a>
</div>
25 changes: 25 additions & 0 deletions app/views/layouts/_user_roles.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div id="user_roles_open_content" style="<%= show_if_opened(status) %>">
<% if UserRole.count > 0 %>
<% UserRole.order(:name).each do |user_role| %>
<a href="<%= user_role_path(user_role) %>" class="menu_link <%= active_link_class(user_role) %>" title="<%= user_role.name %>">
<div class="menu_link_title">
<%= user_role.name %>
</div>
</a>
<% end %>
<% else %>
<div class="menu_item"><%= t("user_role.none") %></div>
<a href="<%= new_user_role_path %>" class="menu_link menu_link_create">
<div class="menu_link_title">
<%= t("user_role.new") %>
</div>
</a>
<% end %>
</div>
<div id="user_roles_closed_content" style="<%=h show_if_closed(status) %>">
<a href="javascript:App.open_menu_box('user_roles')" class="menu_link menu_link_create">
<div class="menu_link_title">
<%= pluralize(UserRole.count, 'user_role')%>
</div>
</a>
</div>
6 changes: 6 additions & 0 deletions app/views/projects/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@
<%= form.text_area :description, :class => "input-xlarge" %>
</div>
</div>
<div class="control-group">
<%= form.label t("project.default_user_role"), :class => "control-label" %>
<div class="controls">
<%= select_tag "user_role", options_for_select(@user_roles, params[:user_role]), :id => "user_role" %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<% end %>

<fieldset class="fieldset">
<legend><%= pluralize Project.count, 'Project' %> listed</legend>
<legend><%= pluralize current_user.projects.count, 'Project' %> listed</legend>

<% if @projects.empty? %>
<div><%= t("project.no_configured") %></div>
Expand Down
7 changes: 7 additions & 0 deletions app/views/user_role_projects/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= form.hidden_field :user_role_id %>
<div class="control-group">
<%= form.label t("user_role_project.project"), :class => "control-label" %>
<div class="controls">
<%= form.select :project_id, @projects, { include_blank: false } %>
</div>
</div>
26 changes: 26 additions & 0 deletions app/views/user_role_projects/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% content_for :breadcrumb do %>
<%= breadcrumb_box do %>
<%= link_to t("user_roles"), user_roles_path %>
<span class="divider">&raquo;</span>
<%= link_to @user_role_project.user_role.name, user_role_path(@user_role_project.user_role) %>
<span class="divider">&raquo;</span>
<%= t("user_role_project.new") %>
<% end %>
<% end %>

<% content_for :page_title do %>
<% @page_title = t("user_role_project.new") %>
<h2><%= t("user_role_project.new") %></h2>
<% end %>

<% flashed_errors(:user_role_project) %>

<div>
<%= form_for @user_role_project, :html => {:class => "form-horizontal"} do |f| %>
<%= render :partial => 'form', :locals => {:form => f} %>
<div class="form-actions">
<%= f.submit t("user_role.create"), class: "btn btn-primary" %>
<%= link_to t("user_role_project.back_to"), user_role_path(@user_role_project.user_role), :class => 'arrow_link ontheright' %>
</div>
<% end %>
</div>
7 changes: 7 additions & 0 deletions app/views/user_role_users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= form.hidden_field :user_role_id %>
<div class="control-group">
<%= form.label t("user_role_user.user"), :class => "control-label" %>
<div class="controls">
<%= form.select :user_id, @users, { include_blank: false } %>
</div>
</div>
Loading