From 7e0cd47ab44389fb9933345407ad2c2cd2a579b6 Mon Sep 17 00:00:00 2001 From: elijah Date: Mon, 11 Apr 2016 14:10:38 -0700 Subject: allow monitor auth to create users even if invites are normally required. --- app/controllers/v1/users_controller.rb | 2 +- app/models/account.rb | 18 +++++++++++++++--- app/models/user.rb | 17 +++++++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) (limited to 'app') diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index 8296eb0..6640d10 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -63,7 +63,7 @@ module V1 # tester auth can only create test users. def create_test_account if User::is_test?(params[:user][:login]) - @user = Account.create(params[:user]) + @user = Account.create(params[:user], :invite_required => false) respond_with @user else head :forbidden diff --git a/app/models/account.rb b/app/models/account.rb index a85e56c..7310250 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -14,13 +14,26 @@ class Account @user = user end + # + # Creates a new user, with matching identity record. + # # Returns the user record so it can be used in views. - def self.create(attrs) + # + # options: + # + # :invite_required -- if 'false', will overrides app-wide + # configuration by same name. + # + def self.create(attrs, options={}) identity = nil user = nil user = User.new(attrs) + if options[:invite_required] == false + user.ignore_invites! + end user.save + # this is not very atomic, but we do the best we can: if !user.is_tmp? && user.persisted? identity = user.identity identity.user_id = user.id @@ -28,8 +41,7 @@ class Account identity.errors.each do |attr, msg| user.errors.add(attr, msg) end - - if APP_CONFIG[:invite_required] + if user.invite_required? user_invite_code = InviteCode.find_by_invite_code user.invite_code user_invite_code.invite_count += 1 user_invite_code.save diff --git a/app/models/user.rb b/app/models/user.rb index 51e9279..cb093cf 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -43,8 +43,7 @@ class User < CouchRest::Model::Base :mx_with_fallback => true - validates_with InviteCodeValidator, on: :create, if: -> {APP_CONFIG[:invite_required]} - + validates_with InviteCodeValidator, :on => :create, :if => :invite_required? timestamps! @@ -57,6 +56,11 @@ class User < CouchRest::Model::Base include TemporaryUser # MUST come after designs are defined. + def initialize(*args) + super + @invite_required = APP_CONFIG[:invite_required] + end + def self.login_starts_with(query) self.by_login.startkey(query).endkey(query + "\ufff0") end @@ -113,6 +117,15 @@ class User < CouchRest::Model::Base false end + def invite_required? + @invite_required + end + + # should only be called for testing or monitoring purposes + def ignore_invites! + @invite_required = false + end + def most_recent_tickets(count=3) Ticket.for_user(self).limit(count).all #defaults to having most recent updated first end -- cgit v1.2.3