From 69eb52f0617e91479cf9011310dcef3170212709 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Tue, 28 Apr 2015 10:34:22 +0000 Subject: [PATCH] Allow supporters to change asciicast visibility --- app/models/asciicast_params.rb | 4 +++- app/models/user.rb | 4 ++++ app/policies/asciicast_policy.rb | 2 +- .../20150428102632_add_supporter_to_users.rb | 5 +++++ ..._asciicasts_private_by_default_to_users.rb | 5 +++++ db/schema.rb | 8 ++++--- spec/policies/asciicast_policy_spec.rb | 21 +++++++++++++++++++ 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20150428102632_add_supporter_to_users.rb create mode 100644 db/migrate/20150428103935_add_asciicasts_private_by_default_to_users.rb diff --git a/app/models/asciicast_params.rb b/app/models/asciicast_params.rb index 80bc09a..bc38f30 100644 --- a/app/models/asciicast_params.rb +++ b/app/models/asciicast_params.rb @@ -2,11 +2,13 @@ class AsciicastParams FormatError = Class.new(StandardError) def self.build(asciicast_params, user, user_agent) - if asciicast_params.try(:respond_to?, :read) + attributes = if asciicast_params.try(:respond_to?, :read) from_format_1_request(asciicast_params, user, user_agent) else from_format_0_request(asciicast_params, user, user_agent) end + + attributes.merge(private: user.new_asciicast_private?) end def self.from_format_0_request(params, user, user_agent) diff --git a/app/models/user.rb b/app/models/user.rb index 457e8f7..de4e5bf 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -130,6 +130,10 @@ class User < ActiveRecord::Base expiring_tokens.count == 1 end + def new_asciicast_private? + supporter? && asciicasts_private_by_default? + end + private def generate_auth_token diff --git a/app/policies/asciicast_policy.rb b/app/policies/asciicast_policy.rb index 7b6bf87..377ca55 100644 --- a/app/policies/asciicast_policy.rb +++ b/app/policies/asciicast_policy.rb @@ -39,7 +39,7 @@ class AsciicastPolicy < ApplicationPolicy def change_visibility? return false unless user - user.admin? + user.admin? || user.supporter? end end diff --git a/db/migrate/20150428102632_add_supporter_to_users.rb b/db/migrate/20150428102632_add_supporter_to_users.rb new file mode 100644 index 0000000..7556c31 --- /dev/null +++ b/db/migrate/20150428102632_add_supporter_to_users.rb @@ -0,0 +1,5 @@ +class AddSupporterToUsers < ActiveRecord::Migration + def change + add_column :users, :supporter, :boolean, null: false, default: false + end +end diff --git a/db/migrate/20150428103935_add_asciicasts_private_by_default_to_users.rb b/db/migrate/20150428103935_add_asciicasts_private_by_default_to_users.rb new file mode 100644 index 0000000..7388622 --- /dev/null +++ b/db/migrate/20150428103935_add_asciicasts_private_by_default_to_users.rb @@ -0,0 +1,5 @@ +class AddAsciicastsPrivateByDefaultToUsers < ActiveRecord::Migration + def change + add_column :users, :asciicasts_private_by_default, :boolean, null: false, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 3b62c01..09f68c5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150401161102) do +ActiveRecord::Schema.define(version: 20150428103935) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -110,12 +110,14 @@ ActiveRecord::Schema.define(version: 20150401161102) do t.string "uid" t.string "email" t.string "name" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "username" t.string "auth_token" t.string "theme_name" t.string "temporary_username" + t.boolean "supporter", default: false, null: false + t.boolean "asciicasts_private_by_default", default: false, null: false end add_index "users", ["auth_token"], name: "index_users_on_auth_token", using: :btree diff --git a/spec/policies/asciicast_policy_spec.rb b/spec/policies/asciicast_policy_spec.rb index aae4713..100578d 100644 --- a/spec/policies/asciicast_policy_spec.rb +++ b/spec/policies/asciicast_policy_spec.rb @@ -90,4 +90,25 @@ describe AsciicastPolicy do end end + permissions :change_visibility? do + it "denies access if user is nil" do + expect(subject).not_to permit(nil, Asciicast.new) + end + + it "grants access if user is admin" do + user = stub_model(User, admin?: true) + expect(subject).to permit(user, Asciicast.new) + end + + it "denies access if user isn't supporter" do + user = stub_model(User, supporter?: false) + expect(subject).not_to permit(user, Asciicast.new) + end + + it "grants access if user is a supporter" do + user = stub_model(User, supporter?: true) + expect(subject).to permit(user, Asciicast.new) + end + end + end