Class: AuthlogicCrowd::CrowdSynchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/authlogic_crowd/crowd_synchronizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, crowd_client, local_record = nil, crowd_record = nil) ⇒ CrowdSynchronizer

Returns a new instance of CrowdSynchronizer.



6
7
8
9
10
11
12
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 6

def initialize(klass, crowd_client, local_record=nil, crowd_record=nil)
  self.klass = klass
  self.crowd_client = crowd_client
  self.local_record = local_record if local_record
  self.crowd_record = crowd_record if crowd_record
  @syncing = false
end

Instance Attribute Details

#crowd_clientObject

Returns the value of attribute crowd_client.



4
5
6
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 4

def crowd_client
  @crowd_client
end

#klassObject

Returns the value of attribute klass.



4
5
6
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 4

def klass
  @klass
end

#local_recordObject

Returns the value of attribute local_record.



4
5
6
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 4

def local_record
  @local_record
end

Instance Method Details

#create_crowd_recordObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 30

def create_crowd_record
  if local_record.before_create_crowd_record
    self.crowd_record = SimpleCrowd::User.new({:username => local_record.send(klass.)})
    if sync_to_crowd(true)
      local_record.after_create_crowd_record
      return crowd_record
    end
  end
  nil
end

#create_record_from_crowdObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 68

def create_record_from_crowd
  self.local_record = klass.new
  if local_record.before_create_from_crowd
    local_record.send(:"#{klass.}=", crowd_record.username) if local_record.respond_to?(:"#{klass.}=")
    sync_from_crowd
    if local_record.save_without_session_maintenance
      local_record.after_create_from_crowd
      return local_record
    end
  end
  nil
end

#crowd_recordObject



20
21
22
23
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 20

def crowd_record
  @crowd_record ||= @local_record.crowd_record if @local_record
  @crowd_record
end

#crowd_record=(val) ⇒ Object



25
26
27
28
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 25

def crowd_record=(val)
  @crowd_record = val
  @local_record.crowd_record = @crowd_record if @local_record
end

#sync_from_crowdObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 81

def sync_from_crowd
  return unless local_record && crowd_record
  return if @syncing

  if local_record.before_sync_from_crowd
    @syncing = true
    begin
      local_record.sync_from_crowd
      local_record.save_without_session_maintenance if local_record.changed?
    ensure
      @syncing = false
    end
    local_record.after_sync_from_crowd
  end
end

#sync_to_crowd(new_record = false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/authlogic_crowd/crowd_synchronizer.rb', line 41

def sync_to_crowd(new_record=false)
  return unless local_record && crowd_record
  return if @syncing

  if local_record.before_sync_to_crowd
    @syncing = true
    begin
      local_record.sync_to_crowd
      if new_record || crowd_record.dirty? || local_record.crowd_password_changed?
        if new_record
          crowd_client.add_user crowd_record, local_record.crowd_password
        else
          crowd_client.update_user crowd_record
          if local_record.crowd_password_changed? && local_record.crowd_password
            crowd_client.update_user_credential crowd_record.username, local_record.crowd_password
          end
        end
      end
    ensure
      @syncing = false
    end
    local_record.after_sync_to_crowd
    return true
  end
  false
end