Skip to content

Commit

Permalink
[copycopter#2] Add username and password to project
Browse files Browse the repository at this point in the history
* Refactoring, formatting and other preparation for feature.
* Whitelist Project attributes
* Authorize project-specific actions
  • Loading branch information
Dan Croak committed Mar 7, 2012
1 parent b57fc56 commit eac08df
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 107 deletions.
8 changes: 2 additions & 6 deletions app/controllers/application_controller.rb
Expand Up @@ -8,19 +8,15 @@ def prefer_html?
end

def set_html_preference(preference)
logger.info preference
logger.info session[:html_preference]
session[:html_preference] = preference
logger.info preference
logger.info session[:html_preference]
end

private

def authorize
def authorize(project)
unless Rails.env.test?
authenticate_or_request_with_http_basic do |username, password|
username == 'copy' && password == 'copter'
username == project.username && password == project.password
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/blurbs_controller.rb
@@ -1,8 +1,7 @@
class BlurbsController < ApplicationController
before_filter :authorize

def destroy
@blurb = Blurb.find(params[:id])
authorize @blurb.project
@blurb.destroy
flash[:notice] = 'Blurb successfully deleted'
redirect_to @blurb.project
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/locales_controller.rb
@@ -1,7 +1,6 @@
class LocalesController < ApplicationController
before_filter :authorize

def new
@project = Project.find(params[:project_id])
authorize @project
end
end
6 changes: 4 additions & 2 deletions app/controllers/projects_controller.rb
@@ -1,6 +1,4 @@
class ProjectsController < ApplicationController
before_filter :authorize, :only => [:show]

def create
@project = Project.new(params[:project])

Expand All @@ -13,12 +11,14 @@ def create

def destroy
@project = Project.find(params[:id])
authorize @project
@project.destroy
redirect_to projects_url
end

def edit
@project = Project.find(params[:id])
authorize @project
end

def new
Expand All @@ -27,6 +27,7 @@ def new

def show
@project = Project.find(params[:id])
authorize @project
@locale = @project.locale(params[:locale_id])

if stale? :etag => @project.etag
Expand All @@ -37,6 +38,7 @@ def show

def update
@project = Project.find(params[:id])
authorize @project

if @project.update_attributes params[:project]
redirect_to @project
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/versions_controller.rb
@@ -1,10 +1,9 @@
class VersionsController < ApplicationController
before_filter :authorize

def create
self.set_html_preference params[:prefer_html]
@localization = Localization.find(params[:localization_id])
@project = @localization.project
authorize @project
@version = @localization.revise(params[:version])
@version.save!

Expand All @@ -22,6 +21,7 @@ def new
@localization = Localization.find(params[:localization_id])
@version = @localization.revise
@project = @localization.project
authorize @project
@locale = @project.locale
render
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/blurb.rb
Expand Up @@ -3,11 +3,14 @@
class Blurb < ActiveRecord::Base
include HTMLDiff

# Associations
belongs_to :project
has_many :localizations, :dependent => :destroy

# Validations
validates_presence_of :project_id

# Callbacks
after_destroy :update_project_caches

def self.ordered
Expand Down
5 changes: 4 additions & 1 deletion app/models/project.rb
@@ -1,6 +1,9 @@
require 'extensions/string'

class Project < ActiveRecord::Base
# Attributes
attr_accessible :name, :password, :username

# Associatons
has_many :blurbs
belongs_to :draft_cache, :class_name => 'TextCache', :dependent => :destroy
Expand All @@ -10,7 +13,7 @@ class Project < ActiveRecord::Base
:dependent => :destroy

# Validations
validates_presence_of :api_key
validates_presence_of :api_key, :name, :password, :username
validates_uniqueness_of :api_key

# Callbacks
Expand Down
4 changes: 3 additions & 1 deletion app/views/projects/_form.html.erb
@@ -1,7 +1,9 @@
<%= form.inputs do %>
<%= form.input :name %>
<%= form.input :username %>
<%= form.input :password %>
<% if project.persisted? -%>
<%= form.input :archived, :hint => t('project.archived.hint', :default => "Archived project don't count against your project total. You cannot create or edit content.") %>
<%= form.input :archived %>
<% end -%>
<% end %>
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -2,9 +2,9 @@
namespace :api do
namespace :v2 do
resources :projects, :only => [] do
resources :deploys, :only => [:create]
resources :draft_blurbs, :only => [:create, :index]
resources :published_blurbs, :only => [:index]
resources :deploys, :only => [:create]
end
end
end
Expand Down
23 changes: 9 additions & 14 deletions db/migrate/20120229031631_create_schema.rb
@@ -1,8 +1,7 @@
class CreateSchema < ActiveRecord::Migration
def up
create_table 'blurbs', :force => true do |t|
t.datetime 'created_at'
t.datetime 'updated_at'
t.timestamps :null => false
t.string 'key'
t.integer 'project_id'
end
Expand All @@ -11,8 +10,7 @@ def up
:name => 'index_blurbs_on_project_id_and_key', :unique => true

create_table 'delayed_jobs', :force => true do |t|
t.datetime 'created_at'
t.datetime 'updated_at'
t.timestamps :null => false
t.integer 'attempts', :default => 0
t.datetime 'failed_at'
t.text 'handler'
Expand All @@ -27,8 +25,7 @@ def up
:name => 'delayed_jobs_priority'

create_table 'locales', :force => true do |t|
t.datetime 'created_at'
t.datetime 'updated_at'
t.timestamps :null => false
t.boolean 'enabled', :default => true, :null => false
t.string 'key'
t.integer 'project_id'
Expand All @@ -38,8 +35,7 @@ def up
:name => 'index_locales_on_project_id_and_key', :unique => true

create_table 'localizations', :force => true do |t|
t.datetime 'created_at'
t.datetime 'updated_at'
t.timestamps :null => false
t.integer 'blurb_id'
t.text 'draft_content', :default => '', :null => false
t.integer 'locale_id'
Expand All @@ -51,26 +47,25 @@ def up
:name => 'index_localizations_on_blurb_id'

create_table 'projects', :force => true do |t|
t.datetime 'created_at'
t.datetime 'updated_at'
t.timestamps :null => false
t.string 'api_key'
t.boolean 'archived', :default => false, :null => false
t.integer 'draft_cache_id'
t.string 'name'
t.string 'password', :default => 'copter', :null => false
t.integer 'published_cache_id'
t.string 'username', :default => 'copy', :null => false
end

add_index 'projects', ['archived'], :name => 'index_projects_on_archived'

create_table 'text_caches', :force => true do |t|
t.datetime 'created_at'
t.datetime 'updated_at'
t.timestamps :null => false
t.text 'data'
end

create_table 'versions', :force => true do |t|
t.datetime 'created_at'
t.datetime 'updated_at'
t.timestamps :null => false
t.text 'content', :default => '', :null => false
t.integer 'localization_id'
t.integer 'number', :null => false
Expand Down
19 changes: 10 additions & 9 deletions spec/models/deploy_job_spec.rb
@@ -1,14 +1,15 @@
require 'spec_helper'

describe DeployJob, "#perform" do
it "deploys its project" do
project = Factory(:project)
project.stubs(:deploy! => true)

job = DeployJob.new(project)
project.should_not have_received(:deploy!)

describe DeployJob, 'perform' do
before do
@project = Factory(:project)
@project.stubs :deploy! => true
job = DeployJob.new(@project)
@project.should_not have_received(:deploy!)
job.perform
project.should have_received(:deploy!)
end

it 'deploys its project' do
@project.should have_received(:deploy!)
end
end

0 comments on commit eac08df

Please sign in to comment.