diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index eebd7e4..e097175 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,4 +3,10 @@ class ApplicationController < ActionController::Base before_filter :authenticate_user! + protected + + def authenticate_admin! + redirect_to(root_path) and return(false) unless user_signed_in? && current_user.admin? + end + end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..5438852 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,29 @@ +class UsersController < ApplicationController + respond_to :html + + before_filter :authenticate_admin! + + def index + @users = User.paginate(:page => params[:page]) + end + + def show + @user = User.find(params[:id]) + end + + def new + end + + def edit + end + + def create + end + + def update + end + + def destroy + end + +end diff --git a/app/models/user.rb b/app/models/user.rb index 58470ec..f1351d6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,6 @@ class User include Mongoid::Document + include Mongoid::Timestamps devise :database_authenticatable, :recoverable, :rememberable, :trackable, @@ -7,6 +8,7 @@ class User field :name field :admin, :type => Boolean, :default => false + key :name validates_presence_of :name diff --git a/app/views/shared/_navigation.html.haml b/app/views/shared/_navigation.html.haml index 4d3b823..84f05a4 100644 --- a/app/views/shared/_navigation.html.haml +++ b/app/views/shared/_navigation.html.haml @@ -1,6 +1,7 @@ #nav-bar %ul //%li= link_to 'Dashboard', admin_dashboard_path, :class => active_if_here(:dashboards) - %li.apps{:class => active_if_here(:apps)}= link_to 'Apps', apps_path - %li.errs{:class => active_if_here(:errs)}= link_to 'Errs', errs_path + %li.apps{:class => active_if_here(:apps)}= link_to 'Apps', apps_path + %li.errs{:class => active_if_here(:errs)}= link_to 'Errs', errs_path + %li.users{:class => active_if_here(:users)}= link_to 'Users', users_path %div.clear \ No newline at end of file diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml new file mode 100644 index 0000000..9124180 --- /dev/null +++ b/app/views/users/index.html.haml @@ -0,0 +1,16 @@ +- content_for :title, 'Users' + +%table + %thead + %tr + %th Name + %th.main Email + %th Admin? + %tbody + - @users.each do |user| + %tr + %td.nowrap= link_to user.name, user_path(user) + %td= user.email + %td= user.admin? ? 'Y' : 'N' += will_paginate @users, :previous_label => '« Previous', :next_label => 'Next »' + \ No newline at end of file diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml new file mode 100644 index 0000000..1ef3754 --- /dev/null +++ b/app/views/users/show.html.haml @@ -0,0 +1,12 @@ +- content_for :title, @user.name + +%table + %tr + %th Email + %td.main= @user.email + %tr + %th Admin? + %td= @user.admin? ? 'Y' : 'N' + %tr + %th Created + %td= @user.created_at.to_s(:micro) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7f8a2af..9f4cd9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,13 +6,14 @@ Errbit::Application.routes.draw do match '/notifier_api/v2/notices' => 'notices#create' match '/deploys.txt' => 'deploys#create' + resources :notices, :only => [:show] + resources :deploys, :only => [:show] + resources :users resources :errs, :only => [:index] do collection do get :all end end - resources :notices, :only => [:show] - resources :deploys, :only => [:show] resources :apps do resources :errs do diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 0000000..dc23f86 --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe UsersController do + + it_requires_authentication + it_requires_admin + + context 'Signed in as an admin' do + before do + sign_in Factory(:admin) + end + + context "GET /users" do + it 'paginates all users' do + users = 3.times.inject(WillPaginate::Collection.new(1,30)) {|page,_| page << Factory.build(:user)} + User.should_receive(:paginate).and_return(users) + get :index + assigns(:users).should == users + end + end + + context "GET /users/:id" do + it 'finds the user' do + user = Factory(:user) + get :show, :id => user.id + assigns(:user).should == user + end + end + end +end diff --git a/spec/factories/user_factories.rb b/spec/factories/user_factories.rb index 7bb7f72..ce2af3f 100644 --- a/spec/factories/user_factories.rb +++ b/spec/factories/user_factories.rb @@ -5,4 +5,8 @@ Factory.define :user do |u| u.email { Factory.next :user_email } u.password 'password' u.password_confirmation 'password' +end + +Factory.define :admin, :parent => :user do |a| + a.admin true end \ No newline at end of file diff --git a/spec/support/macros.rb b/spec/support/macros.rb index 13928e9..fe15d40 100644 --- a/spec/support/macros.rb +++ b/spec/support/macros.rb @@ -13,7 +13,7 @@ def it_requires_authentication(options = {}) } options.reverse_merge!(default_options) - context 'when logged out' do + context 'when signed out' do before do sign_out :user end @@ -25,4 +25,34 @@ def it_requires_authentication(options = {}) end end end +end + +def it_requires_admin(options = {}) + default_options = { + :for => { + :index => :get, + :show => :get, + :new => :get, + :create => :post, + :edit => :get, + :update => :put, + :destroy => :delete + }, + :params => {:id => 'dummyid'} + } + options.reverse_merge!(default_options) + + context 'when signed in as a regular user' do + before do + sign_out :user + sign_in Factory(:user) + end + + options[:for].each do |action, method| + it "#{method.to_s.upcase} #{action} redirects to the root path" do + send(method, action, options[:params]) + response.should redirect_to(root_path) + end + end + end end \ No newline at end of file -- libgit2 0.21.2