├── .document ├── .gitignore ├── .rbenv-version ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.rdoc ├── Rakefile ├── lib ├── sentient_user.rb └── sentient_user │ └── version.rb ├── sentient_user.gemspec └── test ├── helper.rb └── test_sentient_user.rb /.document: -------------------------------------------------------------------------------- 1 | README.rdoc 2 | lib/**/*.rb 3 | bin/* 4 | features/**/*.feature 5 | LICENSE 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## MAC OS 2 | .DS_Store 3 | 4 | ## TEXTMATE 5 | *.tmproj 6 | tmtags 7 | 8 | ## EMACS 9 | *~ 10 | \#* 11 | .\#* 12 | 13 | ## VIM 14 | *.swp 15 | 16 | ## PROJECT::GENERAL 17 | coverage 18 | rdoc 19 | pkg 20 | 21 | ## PROJECT::SPECIFIC 22 | .bundle 23 | vendor 24 | -------------------------------------------------------------------------------- /.rbenv-version: -------------------------------------------------------------------------------- 1 | 2.3.0 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | sudo: false 3 | rvm: 4 | - 1.9.3 5 | - 2.0.0 6 | - 2.1.0 7 | - 2.1.5 8 | - 2.2.0 9 | - 2.3.0 10 | - jruby 11 | before_install: 12 | - gem update bundler 13 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [dbock at javaguy dot org]. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | sentient_user (0.4.0) 5 | railties (>= 3.1) 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | actionpack (4.0.13) 11 | activesupport (= 4.0.13) 12 | builder (~> 3.1.0) 13 | erubis (~> 2.7.0) 14 | rack (~> 1.5.2) 15 | rack-test (~> 0.6.2) 16 | activesupport (4.0.13) 17 | i18n (~> 0.6, >= 0.6.9) 18 | minitest (~> 4.2) 19 | multi_json (~> 1.3) 20 | thread_safe (~> 0.1) 21 | tzinfo (~> 0.3.37) 22 | ansi (1.5.0) 23 | builder (3.1.4) 24 | docile (1.1.5) 25 | erubis (2.7.0) 26 | i18n (0.7.0) 27 | json (1.8.3) 28 | minitest (4.7.5) 29 | minitest_should (0.3.1) 30 | multi_json (1.12.1) 31 | rack (1.5.5) 32 | rack-test (0.6.3) 33 | rack (>= 1.0) 34 | railties (4.0.13) 35 | actionpack (= 4.0.13) 36 | activesupport (= 4.0.13) 37 | rake (>= 0.8.7) 38 | thor (>= 0.18.1, < 2.0) 39 | rake (11.1.2) 40 | rdoc (4.2.2) 41 | json (~> 1.4) 42 | simplecov (0.11.2) 43 | docile (~> 1.1.0) 44 | json (~> 1.8) 45 | simplecov-html (~> 0.10.0) 46 | simplecov-html (0.10.0) 47 | thor (0.19.1) 48 | thread_safe (0.3.5) 49 | turn (0.9.7) 50 | ansi 51 | minitest (~> 4) 52 | tzinfo (0.3.48) 53 | 54 | PLATFORMS 55 | ruby 56 | 57 | DEPENDENCIES 58 | minitest (= 4.7.5) 59 | minitest_should 60 | rake 61 | rdoc 62 | sentient_user! 63 | simplecov 64 | turn 65 | 66 | BUNDLED WITH 67 | 1.13.1 68 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 bokmann 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = sentient_user 2 | 3 | {}[http://travis-ci.org/bokmann/sentient_user] 4 | 5 | How often have you needed to access the current user in your models? This happens often if you are attempting intelligent logging or if you want the models to control some aspect of their access, etc. This is easy hackery that you can find in many blog entries, but I wanted a nice package for it, for several reasons: 6 | 7 | * There are several different ways this can be accomplished, but only the variation that uses Thread.local is safe for use in thread-safe rails. 8 | * Even using Thread.local is considered slightly hacky (but better than monkeypatching ActiveRecord::Base) 9 | * Should fashions change in this area in Rails' future, I want one place to change it, and to keep my methods working. 10 | * This concept generally breaks in the console, where there is no controller to set the user. This gem adds a make_current method on the user instance, so you can choose to find a user and execute the current_user method. That could be really useful in rake tasks, for instance. 11 | 12 | 13 | == Usage 14 | * include SentientUser in your user model 15 | * include SentientController in your application controller 16 | 17 | == Assumptions and limitations 18 | * Your user model is called 'User'. 19 | * You have a current_user method available to your controllers 20 | * You will write your own tests to cover this code. The gem has tests, but its up to you to make sure you use this ability wisely. 21 | 22 | If those assumptions don't work for you, feel free to unpack and hack. It's simple enough code to change. I purposefully didn't want to complicate it with an initializer, several config options, etc. as all that would blur this delightfully simple piece of code. 23 | 24 | 25 | == Recent Changes 26 | This gem has been remarkably stable for many years and many iterations of rails. The recent deprecation of before_filter in favor of before_action is making me update it. 27 | 28 | * 0.4.0 - for use on Rails 5 and beyond. 29 | * 0.3.3 - for versions prior to rails 5. 30 | 31 | 32 | == Note on Patches/Pull Requests 33 | 34 | * Fork the project. 35 | * Make your feature addition or bug fix. 36 | * Add tests for it. This is important so I don't break it 37 | unintentionally in a future version. 38 | * Commit, do not mess with rakefile, version, or history. 39 | (if you want to have your own version, that is fine but please bump the version in a commit by itself which I can ignore when I pull) 40 | * Send me a pull request. Bonus points for topic branches. 41 | 42 | == Copyright 43 | 44 | Copyright (c) 2009-2016 bokmann. See LICENSE for details. 45 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'rake' 3 | require "bundler/gem_tasks" 4 | require 'rake/testtask' 5 | 6 | Rake::TestTask.new(:test) do |test| 7 | test.libs << 'lib' << 'test' 8 | test.pattern = 'test/**/test_*.rb' 9 | test.verbose = true 10 | end 11 | 12 | task :default => :test 13 | 14 | 15 | require File.expand_path('../lib/sentient_user/version.rb', __FILE__) 16 | -------------------------------------------------------------------------------- /lib/sentient_user.rb: -------------------------------------------------------------------------------- 1 | module SentientUser 2 | 3 | def self.included(base) 4 | base.class_eval { 5 | def self.current 6 | Thread.current[:user] 7 | end 8 | 9 | def self.current=(o) 10 | raise(ArgumentError, 11 | "Expected an object of class '#{self}', got #{o.inspect}") unless (o.is_a?(self) || o.nil?) 12 | Thread.current[:user] = o 13 | end 14 | 15 | def make_current 16 | Thread.current[:user] = self 17 | end 18 | 19 | def current? 20 | !Thread.current[:user].nil? && self.id == Thread.current[:user].id 21 | end 22 | 23 | def self.do_as(user, &block) 24 | old_user = self.current 25 | 26 | begin 27 | self.current = user 28 | response = block.call unless block.nil? 29 | ensure 30 | self.current = old_user 31 | end 32 | 33 | response 34 | end 35 | } 36 | end 37 | end 38 | 39 | module SentientController 40 | def self.included(base) 41 | base.class_eval { 42 | before_action do |c| 43 | User.current = c.send(:current_user) 44 | end 45 | } 46 | end 47 | end -------------------------------------------------------------------------------- /lib/sentient_user/version.rb: -------------------------------------------------------------------------------- 1 | module SentientUser 2 | VERSION = "0.4.0" 3 | end -------------------------------------------------------------------------------- /sentient_user.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.expand_path('../lib/sentient_user/version.rb', __FILE__) 3 | 4 | Gem::Specification.new do |gem| 5 | gem.authors = ["bokmann"] 6 | gem.email = ["dbock@javaguy.org"] 7 | gem.description = %q{lets the User model in most authentication frameworks know who is the current user} 8 | gem.summary = %q{A trivial bit of common code} 9 | gem.homepage = "http://github.com/bokmann/sentient_user" 10 | 11 | gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 12 | gem.files = `git ls-files`.split("\n") 13 | gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 14 | gem.name = "sentient_user" 15 | gem.require_paths = ["lib"] 16 | gem.version = SentientUser::VERSION 17 | 18 | gem.add_dependency "railties", ">= 3.1" 19 | gem.add_development_dependency "rake" 20 | gem.add_development_dependency "rdoc" 21 | gem.add_development_dependency "minitest", "4.7.5" 22 | gem.add_development_dependency 'minitest_should' 23 | gem.add_development_dependency 'turn' 24 | 25 | gem.add_development_dependency "simplecov" 26 | 27 | end 28 | 29 | 30 | -------------------------------------------------------------------------------- /test/helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | #require 'test/unit' 3 | require "minitest/autorun" 4 | require "minitest/should" 5 | 6 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 7 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 8 | require 'sentient_user' 9 | 10 | #class Test::Unit::TestCase 11 | #end 12 | 13 | class Person 14 | include SentientUser 15 | end 16 | 17 | class User 18 | include SentientUser 19 | end 20 | 21 | class AnonymousUser < User ; end 22 | 23 | ExceptedWords = %w{ hackery hacky monkeypatching 24 | ActiveRecord SentientUser SentientController 25 | initializer config rakefile bokmann 26 | sublicense MERCHANTABILITY NONINFRINGEMENT img src 27 | } 28 | 29 | def check_spelling_in_file(path_relative_to_gem_root) 30 | path = "#{File.dirname(__FILE__)}/../#{path_relative_to_gem_root}" 31 | begin 32 | aspell_output = `cat #{path} | aspell list` 33 | rescue => err 34 | warn "You probably don't have aspell. On mac: brew install aspell --lang=en" 35 | raise err 36 | end 37 | noticed_words = aspell_output.split($/) 38 | misspellings = noticed_words - ExceptedWords 39 | assert_equal [], misspellings 40 | end 41 | -------------------------------------------------------------------------------- /test/test_sentient_user.rb: -------------------------------------------------------------------------------- 1 | require 'helper' 2 | 3 | class TestTruth < Minitest::Should::TestCase 4 | should "allow making the 'person' class sentient" do 5 | p = Person.new 6 | p.make_current 7 | assert_equal Person.current, p 8 | end 9 | 10 | should "allow making the 'user' class sentient" do 11 | u = User.new 12 | u.make_current 13 | assert_equal User.current, u 14 | end 15 | 16 | should "not allow making Person.current a user" do 17 | assert_raises ArgumentError do 18 | Person.current = User.new 19 | end 20 | end 21 | 22 | should "allow making person.current a person" do 23 | Person.current = Person.new 24 | end 25 | 26 | should "allow subclasses of user to be assigned to user.current" do 27 | User.current = AnonymousUser.new 28 | end 29 | 30 | should "allow execution of a block as a specified user" do 31 | p, p2 = Person.new, Person.new 32 | p.make_current 33 | Person.do_as(p2) { assert_equal Person.current, p2 } 34 | end 35 | 36 | should "should reset the original user after executing a block as a specified user" do 37 | p, p2 = Person.new, Person.new 38 | p.make_current 39 | 40 | Person.do_as(p2) { } 41 | assert_equal Person.current, p 42 | 43 | begin 44 | Person.do_as(p2) { raise "error" } 45 | rescue 46 | assert_equal Person.current, p 47 | end 48 | end 49 | 50 | should "have no spelling errors in its README" do 51 | check_spelling_in_file "README.rdoc" 52 | end 53 | 54 | should "have no spelling errors in the license" do 55 | check_spelling_in_file "LICENSE" 56 | end 57 | end 58 | --------------------------------------------------------------------------------