From 80875d0b1196f8a1df4c0b3cfa297945ce50d867 Mon Sep 17 00:00:00 2001 From: uu59 Date: Thu, 22 May 2014 11:04:24 +0900 Subject: [PATCH] Change user management. just manage password for admin user --- app/controllers/users_controller.rb | 25 +++++++++++++++++-- app/models/user.rb | 3 +++ app/views/shared/_global_nav.html.erb | 2 +- app/views/users/index.html.haml | 3 --- app/views/users/show.html.haml | 22 +++++++++++++++++ config/locales/translation_ja.yml | 29 +++++++++++++++-------- config/routes.rb | 4 +--- db/seeds.rb | 2 +- spec/controllers/users_controller_spec.rb | 5 ---- 9 files changed, 70 insertions(+), 25 deletions(-) delete mode 100644 app/views/users/index.html.haml create mode 100644 app/views/users/show.html.haml delete mode 100644 spec/controllers/users_controller_spec.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bcee8ca..9fe1623 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,7 +1,28 @@ class UsersController < ApplicationController before_action :login_required + before_action :find_user - def index - @users = User.all + def show + end + + def update + unless @user.authenticate(user_params[:current_password]) + @user.errors.add(:current_password, :wrong_password) + return render :show + end + unless @user.update_attributes(user_params) + return render :show + end + redirect_to misc_user_path + end + + private + + def find_user + @user = User.first # user is only "admin" + end + + def user_params + params.require(:user).permit(:current_password, :password, :password_confirmation) end end diff --git a/app/models/user.rb b/app/models/user.rb index 70c745a..2a4e75a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,8 +1,11 @@ class User < ActiveRecord::Base has_secure_password + attr_accessor :current_password + validates :name, uniqueness: true, presence: true validates :remember_token, uniqueness: true, allow_nil: true + validates :password, length: { minimum: 8 } def generate_remember_token begin diff --git a/app/views/shared/_global_nav.html.erb b/app/views/shared/_global_nav.html.erb index 588e16c..062733e 100644 --- a/app/views/shared/_global_nav.html.erb +++ b/app/views/shared/_global_nav.html.erb @@ -24,7 +24,7 @@ <%= link_to_other t("miscs.information.page_title"), information_misc_path %>
  • - <%= link_to_other t('miscs.users.page_title'), misc_users_path %> + <%= link_to_other t('users.show.page_title'), misc_user_path %>
  • diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml deleted file mode 100644 index 1c8fa6c..0000000 --- a/app/views/users/index.html.haml +++ /dev/null @@ -1,3 +0,0 @@ -%ul - - @users.each do |user| - %li= user.name diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml new file mode 100644 index 0000000..5ce4ba4 --- /dev/null +++ b/app/views/users/show.html.haml @@ -0,0 +1,22 @@ +- page_title t('.page_title') + + + +%div.col-lg-6 + - @user.errors.full_messages.each do |e| + = e + + = form_for(:user, url: misc_user_path, method: :patch) do |f| + %div.form-group + = f.label :name + = f.text_field :name, class: "form-control", disabled: true + %div.form-group + = f.label :current_password + = f.password_field :current_password, class: "form-control" + %div.form-group + = f.label :password + = f.password_field :password, class: "form-control" + %div.form-group + = f.label :password_confirmation + = f.password_field :password_confirmation, class: "form-control" + = f.submit diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index 9bda968..0321964 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -17,7 +17,7 @@ ja: no_alert: なし plugins: - common: &common + common: &plugin_common <<: *terms name: プラグイン名 status: 状態 @@ -30,23 +30,25 @@ ja: recommended: &recommended おすすめプラグイン updated: &updated 更新のあったプラグイン installed: - <<: *common + <<: *plugin_common page_title: *installed recommended: - <<: *common + <<: *plugin_common page_title: *recommended updated: - <<: *common + <<: *plugin_common page_title: *updated - miscs: - common: &common - <<: *terms - users: - <<: *common + users: &users + <<: *terms + show: page_title: ユーザー管理 + + miscs: + common: &misc_common + <<: *terms information: - <<: *common + <<: *misc_common env: 環境変数 env_key: キー env_value: 値 @@ -60,6 +62,10 @@ ja: login_failed: ログインに失敗しました。 activerecord: + errors: + messages: + wrong_password: が違います + models: user: user #g @@ -68,3 +74,6 @@ ja: name: name #g password_digest: password_digest #g remember_token: remember_token #g + current_password: 現在のパスワード + password: パスワード + password_confirmation: パスワード(確認) diff --git a/config/routes.rb b/config/routes.rb index 54d713c..d1137e9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,7 +12,6 @@ Rails.application.routes.draw do end end - resources :users resource :sessions resources :plugins do @@ -29,7 +28,6 @@ Rails.application.routes.draw do resource :misc do get "information" - resources :users do - end + resource :user, only: [:show, :edit, :update] end end diff --git a/db/seeds.rb b/db/seeds.rb index e4c11f2..36c9efa 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,4 +6,4 @@ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) -User.create(name: "admin", password: "changeme") +User.create!(name: "admin", password: "changeme") diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb deleted file mode 100644 index 142455c..0000000 --- a/spec/controllers/users_controller_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe UsersController do - -end