Permalink
Browse files

added more external API methods for user setup or domain

  • Loading branch information...
1 parent 9c1d6ba commit 815898bfff89e51e9308116085758786f25f2562 @mkristian committed Jun 4, 2013
View
6 app/controllers/remote/domains_controller.rb
@@ -6,4 +6,10 @@ def last_changes
respond_with serializer( @domains ).use( :update )
end
+ # GET /domains/last_changes_of_app
+ def last_changes_of_app
+ @domains = Domain.all_changed_after_for_app( params[ :updated_at ], remote_permission )
+ respond_with serializer( @domains ).use( :update )
+ end
+
end
View
13 app/controllers/remote/users_controller.rb
@@ -5,4 +5,17 @@ def last_changes
@users = User.all_changed_after( params[ :updated_at ] )
respond_with serializer( @users ).use( :update )
end
+
+ # GET /users/last_changes_of_app
+ def last_changes_of_app
+ @users = User.all_changed_after_for_app( params[ :updated_at ], remote_permission )
+ respond_with serializer( @users ).use( :for_app )
+ end
+
+ def setup
+ @user = User.filtered_setup( params,
+ remote_permission,
+ User.system_user )
+ respond_with serializer( @user ).use( :for_app )
+ end
end
View
14 app/models/domain.rb
@@ -6,12 +6,22 @@ class Domain < ActiveRecord::Base
validates :modified_by_id, :presence => true
validates :name, :presence => true, :format => /\A[A-Za-z0-9\.]+\z/, :length => { :maximum => 32 }
- def self.all_changed_after(from)
+ def self.all_changed_after( from )
unless from.blank?
- Domain.all(:conditions => ["updated_at > ?", from])
+ Domain.all( :conditions => [ "updated_at > ?", from ] )
else
Domain.all
end
end
+ def self.all_changed_after_for_app( from, app )
+ unless from.blank?
+ DomainsGroupsUser.uniq.joins( :group => :application ).where( 'application_id = ? and users.updated_at > ?',
+ app.id,
+ from )
+ else
+ DomainsGroupsUser.uniq.joins( :group => :application ).where( 'application_id = ?',
+ app.id )
+ end
+ end
end
View
61 app/models/user.rb
@@ -122,6 +122,44 @@ def self.filtered_find(id, current_user)
filtered(find(id), current_user)
end
+ def self._groups( params, application )
+ params[ :groups ].select do |g|
+ group = ( Group.where( :id => g[ :id ] ) + Group.where( :name => g[ :name ] ) ).first
+ if group && group.application == application
+ g[ :id ] = group.id
+ end
+ end if params[ :groups ]
+ end
+
+ def self._user( login, email, name, application )
+ u = self.where( "login=? or email=?", login, email ).first
+ if u
+ u.name = name if name
+ u.email = email if email
+ u.groups.delete_if { |g| g.application != application }
+ else
+ u = self.new( :login => login, :email => email, :name => name )
+ end
+ u.modified_by = self.system_user
+ u.save
+ u
+ end
+
+ def self.filtered_setup( params, application, current_user )
+ groups = self._groups( params, application )
+
+ user = self._user( params[ :login ], params[ :email ],
+ params[ :name ], application )
+
+ user.deep_update_attributes( { :groups => groups },
+ current_user ) if groups
+ user
+ end
+
+ def self.system_user
+ self.first # assuming first == root or system
+ end
+
def self.filtered(user, current_user)
unless current_user.root?
# restrict user to AT unless current_user is user_admin
@@ -176,9 +214,9 @@ def deep_update_attributes(params, current_user)
group_ids = user_manager.group_ids(self,
:groups => groups,
:group_ids => group_ids)
-
params[:group_ids] = group_ids
- update_attributes(params) && user_manager.update(self)
+ update_attributes(params)
+ valid? && user_manager.update(self)
end
public
@@ -220,7 +258,18 @@ def reset_password_and_save
end
end
- def self.all_changed_after(from, at_only = false)
+ def self.all_changed_after_for_app( from, app )
+ unless from.blank?
+ User.uniq.joins( :groups => :application ).where( 'application_id = ? and users.updated_at > ?',
+ app.id,
+ from )
+ else
+ User.uniq.joins( :groups => :application ).where( 'application_id = ?',
+ app.id )
+ end
+ end
+
+ def self.all_changed_after( from, at_only = false )
unless from.blank?
if at_only
User.joins(:groups).where('groups.id = ? AND users.updated_at > ?', Group.AT.id, from)
@@ -258,7 +307,11 @@ def password
end
def root?
- @is_root ||= groups.member? Group.ROOT
+ @is_root ||= ( groups.member?( Group.ROOT ) || system? )
+ end
+
+ def system?
+ @is_system = self.id == self.class.first.id
end
def app_admin?
View
17 app/serializers/user_serializer.rb
@@ -26,6 +26,20 @@ class UserSerializer < Ixtlan::Babel::Serializer
}
})
+ add_context(:for_app,
+ :only => [:id, :login, :name, :email, :updated_at],
+ :include=> {
+ :groups => {
+ :only => [:id, :name],
+ :methods => [ :domains ],
+ :include => {
+ :domains => {
+ :only => [:id, :name]
+ }
+ }
+ }
+ })
+
add_context(:update,
:only => [:id, :login, :name, :updated_at])
@@ -73,10 +87,9 @@ class UserSerializer < Ixtlan::Babel::Serializer
def setup_associations(options = {})
methods = ((((options || {})[:include] || {})[:groups] || {})[:methods] || [])
-
[:applications, :application_ids, :domains, :locales, :regions ].each do |m|
- to_model.groups.each { |g| g.send( m, self) } if methods.member? m
+ to_model.groups.each { |g| g.send( m, self ) } if methods.member? m
end
end
View
3 config/routes.rb
@@ -4,7 +4,10 @@
get '/applications/last_changes', :controller => 'remote/applications', :action => :last_changes
get '/regions/last_changes', :controller => 'remote/regions', :action => :last_changes
get '/users/last_changes', :controller => 'remote/users', :action => :last_changes
+ get '/users/last_changes_for_app', :controller => 'remote/users', :action => :last_changes_for_app
+ put '/users/setup', :controller => 'remote/users', :action => :setup
get '/domains/last_changes', :controller => 'remote/domains', :action => :last_changes
+ get '/domains/last_changes_for_app', :controller => 'remote/domains', :action => :last_changes_for_app
post '/authentications', :controller => 'remote/authentications', :action => :create
post '/authentications/reset_password', :controller => 'remote/authentications', :action => :reset_password
View
74 spec/models/user_spec.rb
@@ -144,10 +144,80 @@
u.reload.groups.should == [@g1]
end
+ it 'should setup a valid user with groups' do
+ u = User.filtered_setup({ :login => 'user2',
+ :name => 'User',
+ :email => 'user2@example.com',
+ :groups => [{:id => @g1.id},
+ {:name => @g2.name}]},
+ @a2,
+ subject)
+ u = u.reload
+ u.groups.member?(@g1).should be_false
+ u.groups.member?(@g2).should be_true
+ u.groups.size.should == 1
+ u.name.should == 'User'
+
+ # just change name + email
+ u = User.filtered_setup({ :login => 'user2',
+ :name => 'user',
+ :email => 'user2.user@example.com' },
+ @a2,
+ subject)
+ u = u.reload
+ u.name.should == 'user'
+ u.email.should == 'user2.user@example.com'
+
+ # remove groups
+ u = User.filtered_setup({ :login => 'user2',
+ :groups => [] },
+ @a2,
+ subject)
+ u = u.reload
+ u.groups.member?(@g1).should be_false
+ u.groups.member?(@g2).should be_false
+ u.groups.size.should == 0
+ end
+
+ it 'should setup a valid user with groups and applications' do
+ u = User.filtered_setup({ :login => 'user2',
+ :name => 'User2',
+ :email => 'user2@example.com',
+ :groups => [{ :name => @g2.name,
+ :application_ids => [ @a2.id ] }]},
+ @a2,
+ User.first ) #root user
+ u = u.reload
+ u.groups.member?(@g1).should be_false
+ u.groups.member?(@g2).should be_true
+ u.groups.size.should == 1
+ u.name.should == 'User2'
+ u.email.should == 'user2@example.com'
+
+ # application associations are restricted to root and app_admin users
+ # so bypass that restriction
+ apps = ApplicationsGroupsUser.where( :user_id => u.id ).collect { |a| a.application }
+ apps.first.should == @a2
+ apps.size.should == 1
+ u = User.filtered_setup({ :login => 'user2',
+ :name => 'User2',
+ :email => 'user2@example.com',
+ :groups => [{ :name => @g2.name,
+ :application_ids => [] }]},
+ @a2,
+ User.first ) #root user
+ u = u.reload
+ u.groups.size.should == 1
+ # application associations are restricted to root and app_admin users
+ # so bypass that restriction
+ apps = ApplicationsGroupsUser.where( :user_id => u.id ).collect { |a| a.application }
+ apps.size.should == 0
+ end
+
it 'should create a valid user with groups' do
- u = User.filtered_new({ :login => 'user2',
+ u = User.filtered_new({ :login => 'user3',
:name => 'User',
- :email => 'user2@example.com',
+ :email => 'user3@example.com',
:groups => [{:id => @g1.id},
{:id => @g2.id}]},
subject)
View
13 spec/serializers/user_serializer_spec.rb
@@ -23,6 +23,19 @@
user.key?('at_token').should == true
end
+ it "use setup" do
+ body = JSON.parse( subject.use( :for_app ).to_json )
+ user = body['user']
+ #puts user.to_yaml
+ user.should have(6).items
+ user['id'].should_not be_nil
+ user['name'].should_not be_nil
+ user['login'].should_not be_nil
+ user['email'].should_not be_nil
+ user['groups'].should_not be_nil
+ user['updated_at'].should_not be_nil
+ end
+
it "use update" do
body = JSON.parse(subject.use(:update).to_json)
user = body['user']

0 comments on commit 815898b

Please sign in to comment.