Allow supporters to change asciicast visibility

element
Marcin Kulik 9 years ago
parent 946904ddfd
commit 69eb52f061

@ -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)

@ -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

@ -39,7 +39,7 @@ class AsciicastPolicy < ApplicationPolicy
def change_visibility?
return false unless user
user.admin?
user.admin? || user.supporter?
end
end

@ -0,0 +1,5 @@
class AddSupporterToUsers < ActiveRecord::Migration
def change
add_column :users, :supporter, :boolean, null: false, default: false
end
end

@ -0,0 +1,5 @@
class AddAsciicastsPrivateByDefaultToUsers < ActiveRecord::Migration
def change
add_column :users, :asciicasts_private_by_default, :boolean, null: false, default: false
end
end

@ -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

@ -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

Loading…
Cancel
Save