Class: Sixsense::AuthController

Inherits:
Object
  • Object
show all
Defined in:
lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb

Class Method Summary collapse

Class Method Details

.is_adminObject

TODO: Convert to genuine Goldberg role check?



11
12
13
14
15
16
17
18
19
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 11

def self.is_admin
  if Goldberg.user
    if Goldberg.user.role.id == 3#4
      #logger.info "User is admin"
      return true
    end
  end
  false
end

.is_site_adminObject



21
22
23
24
25
26
27
28
29
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 21

def self.is_site_admin
  if Goldberg.user
    if [3].include? Goldberg.user.role.id # , 4
      #logger.info "User is site admin"
      return true
    end
  end
  false
end

.loggerObject



6
7
8
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 6

def self.logger
  ActionController::Base.logger
end

.login_goldberg(cookies, session) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 86

def self.(cookies, session)
  return unless (cookies[:goldberg_user_id] && cookies[:goldberg_user_password])

  begin
    user = Goldberg::User.find(cookies[:goldberg_user_id])
    if Digest::MD5.hexdigest( user.password + COOKIE_SALT2 ) == cookies[:goldberg_user_password]
      Goldberg.user = user
      logger.info "Found goldberguser by cookie!"
    else
      logger.info "WARNING: Found goldberguser by cookie, but hash mismatch!"
    end
  rescue => e
    logger.info "WARNING: Something went wrong during login_goldberg: ERROR: #{e.class} #{e.message} #{e.backtrace.join("\n")}"
  end
end

.login_vb(cookies, session) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 102

def self.(cookies, session)
  return unless (cookies[:bbuserid] && cookies[:bbpassword])
  forumuser = nil
  begin
    forumuser = Remote::Vbulletin::User.find(cookies[:bbuserid])
  rescue => e
    logger.info "WARNING: Something went wrong during login_vb: ERROR: #{e.class} #{e.message} #{e.backtrace.join("\n")}"
  end
  return unless forumuser
  if forumuser.banned?
    logger.info "User is banned!!"
    return
  end
  if forumuser.sixuser.nil?
    logger.info "Has no sixuser"
    return
  end
  if Digest::MD5.hexdigest( forumuser.password + COOKIE_SALT ) == cookies[:bbpassword]
    Sixsense.user = forumuser.sixuser
    logger.info "Found forumuser by cookie!"
  else
    logger.info "WARNING: Found forumuser by cookie, but hash mismatch!"
  end
end

.logout(cookies) ⇒ Object



31
32
33
34
35
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 31

def self.logout(cookies)
  cookies.delete :goldberg_user_id if cookies[:goldberg_user_id]
  cookies.delete :goldberg_user_password if cookies[:goldberg_user_password]
  cookies.delete :sixsense if cookies[:sixsense]
end

.process_login(cookies, session) ⇒ Object



37
38
39
40
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 37

def self.(cookies, session)
  session[:sixsense] ||= Hash.new
  Sixsense.user = if session[:sixsense][:user_id]
    logger.info "Finding user by session"
    Sixuser.find(session[:sixsense][:user_id])
  else
    nil
  end

  # Try login from Goldberg Cookie when available
  (cookies, session) unless Goldberg.user

  # Try to find Sixuser from Goldberg.user or vB cookie
  unless Sixsense.user
    if Goldberg.user
      Sixsense.user = Sixuser.find_by_goldberguser_id(Goldberg.user.id)
      logger.info "Found sixuser by goldbergid" if Sixsense.user
    end
    unless Sixsense.user
      # Else, Try login from vBulletin Cookie when available
      (cookies, session)
    end
  end

  #unless Goldberg.user
  # DISABLED: No logging into Goldberg from vB or anything for now
  #if Sixsense.user
  #  if Sixsense.user.goldberguser
  #    Goldberg.user = Sixsense.user.goldberguser
  #  end
  #end
  #end

  if Sixsense.user
    Sixsense.user.last_activity = Time.now # TODO: Evaluate if we should possibly only do this on specific actions ?
    Sixsense.user.save
    session[:sixsense][:user_id] ||= Sixsense.user.id
    session[:sixsense][:timezoneoffset] = if Sixsense.user.forumuser
      offset = Sixsense.user.forumuser.timezoneoffset.to_f
      offset unless offset == 0.0
    end
  end

  session[:sixsense][:timezoneoffset] ||= "London"
  Time.zone = session[:sixsense][:timezoneoffset]
  Sixsense.zone_id = Sixsense.calc
  logger.info "#{Sixsense.user.name} (#{Sixsense.user.forumuser.id if Sixsense.user.forumuser}): #{Time.zone} - #{Sixsense.zone_id}" if Sixsense.user
end


127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 127

def self.save_goldberg_cookie(cookies)
  cookies[:goldberg_user_id] = {
    :value => Goldberg.user.id,
    :DOMAIN => DOMAIN,
    :expires => 1.year.from_now
  }
  cookies[:goldberg_user_password] = {
    :value => Digest::MD5.hexdigest( Goldberg.user.password + COOKIE_SALT2 ),
    :DOMAIN => DOMAIN,
    :expires => 1.year.from_now
  }
end


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/six-updater-web/vendor/plugins/six-query/app/controllers/sixsense/auth_controller.rb', line 140

def self.save_vb_cookie(cookies)
  if Sixsense.user
    # Workaround for vBulletin missing
    begin
      if Sixsense.user.forumuser
        unless Sixsense.user.forumuser.banned?
          cookies[:bbuserid] = {
            :value => Sixsense.user.forumuser.userid,
            :DOMAIN => DOMAIN,
            :expires => 1.year.from_now
          }
          cookies[:bbpassword] = {
            # TODO: Shouldn't this be the password in Goldberg ? :D
            :value => Digest::MD5.hexdigest( Sixsense.user.forumuser.password + COOKIE_SALT ),
            :DOMAIN => DOMAIN,
            :expires => 1.year.from_now
          }
        end
      end
    rescue => e
      logger.info "WARNING: Something went wrong during save_vb_cookie: ERROR: #{e.class} #{e.message} #{e.backtrace.join("\n")}"
    end
  end
end