Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
SocialStream::Models::Subject
Defined in:
app/models/user.rb

Overview

Every social network must have users, and a social network builder couldn’t be the exception.

Social Stream uses the awesome gem github.com/plataformatec/devise for managing authentication

Almost all the logic of the interaction between User and the rest of classes in Social Stream is done through Actor. The glue between User and Actor is in SocialStream::Models::Subject

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SocialStream::Models::Subject

#to_param

Class Method Details

.find_for_authentication(conditions) ⇒ Object

presence ID and login



91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/user.rb', line 91

def find_for_authentication(conditions)
  if (  = conditions[:email] ).present?
    if  =~ /@/
      find_by_email()
    else
      find_by_slug()
    end
  else
    super
  end
end

.find_or_create_for_oauth(info, signed_in_user = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/user.rb', line 132

def find_or_create_for_oauth(info, signed_in_user = nil)
  auth = Authentication.find_by_provider_and_uid(info["provider"], info["uid"])

  if auth.present?
    if signed_in_user.present? && auth.user != signed_in_user
      raise "Authenticated users mismatch: signed_in: #{ signed_in_user.id }, auth: #{ auth.user_id }"
    end

    return auth.user
  end

  user = signed_in_user ||
    info["info"]["email"].present? && User.find_by_email(info["info"]["email"]) ||
    User.create!(name: info["info"]["name"],
                 email: info["info"]["email"] || "no-reply-#{ Devise.friendly_token }@demo-social-stream.dit.upm.es", # TODO: split credentails from validation
                 password: Devise.friendly_token[0,20])


  Authentication.create! user:     user,
                         uid:      info["uid"],
                         provider: info["provider"]
  user
end

.find_or_initialize_with_error_by_email(value, error) ⇒ Object

Overwrite devise default method to support finding with actor.email



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/user.rb', line 112

def find_or_initialize_with_error_by_email(value, error)
  if value.present?
    record = find_by_email(value)
  end
  
  unless record
    record = new
    
    if value.present?
      record.email = value
    else
      error = :blank
    end
    
    record.errors.add(:email, error)
  end
  
  record
end

.find_or_initialize_with_errors(required_attributes, attributes, error = :invalid) ⇒ Object



103
104
105
106
107
108
109
# File 'app/models/user.rb', line 103

def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
  if required_attributes == [:email]
    find_or_initialize_with_error_by_email(attributes[:email], error)
  else
    super
  end
end

Instance Method Details

#as_json(options = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'app/models/user.rb', line 51

def as_json options = nil
  {
    id: id,
    actorId: actor_id,
    nickName: slug,
    displayName: name,
    email: email,
  }
end

#representedObject

Subjects this user can acts as



41
42
43
44
45
46
47
48
49
# File 'app/models/user.rb', line 41

def represented
  candidates = contact_actors(:direction => :sent).map(&:id)

  contact_subjects(:direction => :received) do |q|
    q.joins(:sent_ties => { :relation => :permissions }).
      merge(Permission.represent).
      where(:id => candidates)
  end
end