From 592a3f99b5b63bf1631f4df93d81e5a006a623cf Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 27 Apr 2012 14:47:22 -0700 Subject: [PATCH] Role system to replace the yml admins and community spotlight. we can also now add a beta role --- app/assets/stylesheets/scaffolds.css.scss | 56 ++++++++++++++++++++ app/models/account_deleter.rb | 2 +- app/models/person.rb | 5 +- app/models/role.rb | 36 +++++++++++++ app/models/user.rb | 3 +- db/migrate/20120427152648_create_roles.rb | 10 ++++ db/schema.rb | 9 +++- spec/controllers/admins_controller_spec.rb | 6 +-- spec/controllers/contacts_controller_spec.rb | 2 +- spec/controllers/streams_controller_spec.rb | 2 +- spec/models/person_spec.rb | 10 ++-- spec/models/role_spec.rb | 5 ++ 12 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 app/assets/stylesheets/scaffolds.css.scss create mode 100644 app/models/role.rb create mode 100644 db/migrate/20120427152648_create_roles.rb create mode 100644 spec/models/role_spec.rb diff --git a/app/assets/stylesheets/scaffolds.css.scss b/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 000000000..05188f08e --- /dev/null +++ b/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,56 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; } + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; } + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; } + +a { + color: #000; + &:visited { + color: #666; } + &:hover { + color: #fff; + background-color: #000; } } + +div { + &.field, &.actions { + margin-bottom: 10px; } } + +#notice { + color: green; } + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; } + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; } + ul li { + font-size: 12px; + list-style: square; } } diff --git a/app/models/account_deleter.rb b/app/models/account_deleter.rb index f6d8bf45d..c3565994d 100644 --- a/app/models/account_deleter.rb +++ b/app/models/account_deleter.rb @@ -103,7 +103,7 @@ class AccountDeleter end def normal_ar_person_associates_to_delete - [:posts, :photos, :mentions, :participations] + [:posts, :photos, :mentions, :participations, :roles] end def ignored_or_special_ar_person_associations diff --git a/app/models/person.rb b/app/models/person.rb index 6f0733d35..015e7f45f 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -36,6 +36,7 @@ class Person < ActiveRecord::Base accepts_nested_attributes_for :profile before_validation :downcase_diaspora_handle + def downcase_diaspora_handle diaspora_handle.downcase! unless diaspora_handle.blank? end @@ -46,6 +47,8 @@ class Person < ActiveRecord::Base has_many :comments, :foreign_key => :author_id, :dependent => :destroy # This person's own comments has_many :participations, :foreign_key => :author_id, :dependent => :destroy + has_many :roles + belongs_to :owner, :class_name => 'User' has_many :notification_actors @@ -88,7 +91,7 @@ class Person < ActiveRecord::Base } def self.community_spotlight - AppConfig[:community_spotlight].present? ? Person.where(:diaspora_handle => AppConfig[:community_spotlight]) : [] + Person.joins(:roles).where(:roles => {:name => 'spotlight'}) end # Set a default of an empty profile when a new Person record is instantiated. diff --git a/app/models/role.rb b/app/models/role.rb new file mode 100644 index 000000000..57eb50d65 --- /dev/null +++ b/app/models/role.rb @@ -0,0 +1,36 @@ +#NOTE add the person object you want to attach role to... +class Role < ActiveRecord::Base + belongs_to :person + + def self.is_admin?(person) + find_by_person_id_and_name(person.id, 'admin') + end + + def self.add_beta(person) + find_or_create_by_person_id_and_name(person.id, 'beta') + end + + def self.add_admin(person) + find_or_create_by_person_id_and_name(person.id, 'admin') + end + + def self.add_spotlight(person) + find_or_create_by_person_id_and_name(person.id, 'spotlight') + end + + def self.load_admins + admins = AppConfig[:admins] || [] + admins.each do |username| + u = User.find_by_username(username) + find_or_create_by_person_id_and_name(u.person.id, 'admin') + end + end + + def self.load_spotlight + spotlighters = AppConfig[:community_spotlight] || [] + spotlighters.each do |diaspora_handle| + person = Person.find_by_diaspora_handle(diaspora_handle) + find_or_create_by_person_id_and_name(person.id, 'spotlight') + end + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index be10ea80d..62a3a472e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -430,9 +430,10 @@ class User < ActiveRecord::Base end def admin? - AppConfig[:admins].present? && AppConfig[:admins].include?(self.username) + Role.is_admin?(self.person) end + def guard_unconfirmed_email self.unconfirmed_email = nil if unconfirmed_email.blank? || unconfirmed_email == email diff --git a/db/migrate/20120427152648_create_roles.rb b/db/migrate/20120427152648_create_roles.rb new file mode 100644 index 000000000..8f7dcdd56 --- /dev/null +++ b/db/migrate/20120427152648_create_roles.rb @@ -0,0 +1,10 @@ +class CreateRoles < ActiveRecord::Migration + def change + create_table :roles do |t| + t.integer :person_id + t.string :name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e61777389..1df52e628 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120422072257) do +ActiveRecord::Schema.define(:version => 20120427152648) do create_table "account_deletions", :force => true do |t| t.string "diaspora_handle" @@ -388,6 +388,13 @@ ActiveRecord::Schema.define(:version => 20120422072257) do add_index "rails_admin_histories", ["item", "table", "month", "year"], :name => "index_rails_admin_histories" + create_table "roles", :force => true do |t| + t.integer "person_id" + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "service_users", :force => true do |t| t.string "uid", :null => false t.string "name", :null => false diff --git a/spec/controllers/admins_controller_spec.rb b/spec/controllers/admins_controller_spec.rb index 4f269d419..c1fd96123 100644 --- a/spec/controllers/admins_controller_spec.rb +++ b/spec/controllers/admins_controller_spec.rb @@ -20,7 +20,7 @@ describe AdminsController do context 'admin signed in' do before do - AppConfig[:admins] = [@user.username] + Role.add_admin(@user.person) end it 'succeeds and renders user_search' do @@ -70,7 +70,7 @@ describe AdminsController do context 'admin signed in' do before do - AppConfig[:admins] = [@user.username] + Role.add_admin(@user.person) end it 'does not die if you do it twice' do @@ -90,7 +90,7 @@ describe AdminsController do describe '#stats' do before do - AppConfig[:admins] = [@user.username] + Role.add_admin(@user.person) end it 'succeeds and renders stats' do diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb index 165385f81..91be1c5dc 100644 --- a/spec/controllers/contacts_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -96,7 +96,7 @@ describe ContactsController do end it 'gets queries for users in the app config' do - AppConfig[:community_spotlight] = [alice.diaspora_handle] + Role.add_spotlight(alice.person) get :spotlight assigns[:people].should == [alice.person] diff --git a/spec/controllers/streams_controller_spec.rb b/spec/controllers/streams_controller_spec.rb index 211d196bb..7b1cdef12 100644 --- a/spec/controllers/streams_controller_spec.rb +++ b/spec/controllers/streams_controller_spec.rb @@ -11,7 +11,7 @@ describe StreamsController do describe "#public" do it 'will succeed if admin' do - AppConfig[:admins] = [alice.username] + Role.add_admin(alice.person) get :public response.should be_success end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index ffce24664..7eec55051 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -476,13 +476,11 @@ describe Person do describe '.community_spotlight' do describe "when the pod owner hasn't set up any community spotlight members" do - before do - @existing_community_spotlight = AppConfig[:community_spotlight] - AppConfig[:community_spotlight] = nil - end - after do - AppConfig[:community_spotlight] = @existing_community_spotlight + it 'returns people with the community spotlight role' do + Role.add_spotlight(bob.person) + Person.community_spotlight.should be_present end + it "returns an empty array" do Person.community_spotlight.should == [] end diff --git a/spec/models/role_spec.rb b/spec/models/role_spec.rb new file mode 100644 index 000000000..b575576c3 --- /dev/null +++ b/spec/models/role_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Role do + pending "add some examples to (or delete) #{__FILE__}" +end