user.rb 3.61 KB
Newer Older
Tomasz Gubała's avatar
Tomasz Gubała committed
1 2 3 4
class User < ActiveRecord::Base

  devise :database_authenticatable
  devise :omniauthable
5
  devise :timeoutable
Tomasz Gubała's avatar
Tomasz Gubała committed
6 7

  has_and_belongs_to_many :user_teams, join_table: :user_team_users
8
  has_one :user_data, dependent: :destroy
Tomasz Gubała's avatar
Tomasz Gubała committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22


  def self.find_for_open_id(access_token, signed_in_resource=nil)
    data = access_token.info

    if user = User.where(login: data['nickname'] || data['email']).first
      user.sign_in_count += 1
    else
      user = User.new
      user.sign_in_count = 1
    end

    user.login = data['nickname'] || data['email']
    user.email = data['email']
23 24 25 26 27 28
    if Rails.env.production?
      user.proxy = data['proxy'] + data['userCert'] + data['proxyPrivKey']
    else
      # NOTE simple solution for development setups ("simulating" openid interaction)
      user.proxy = File.read Rails.configuration.secret_proxy_path
    end
Tomasz Gubała's avatar
Tomasz Gubała committed
29
    user.dn = data['dn']
30 31
    user.not_before = OpenSSL::X509::Certificate.new(data['proxy']).not_before
    user.not_after = OpenSSL::X509::Certificate.new(data['proxy']).not_after
Tomasz Gubała's avatar
Tomasz Gubała committed
32 33 34 35 36 37 38 39 40

    *rest, user.surname = data['name'].split
    user.name = rest.join(' ')

    # [UserTeams] At this point it is necessary to obtain the names of teams the user belongs to
    # ... This information must be locally updated, possibly by extending access_token
    # ... (or via a separate method).
    update_user_teams(user, data['userteams'].split(',')) unless data['userteams'].nil?

Tomasz Gubała's avatar
Tomasz Gubała committed
41 42
    user.save

43 44
    user.create_user_data unless user.user_data

gubala's avatar
gubala committed
45
    logger.info Time.now.to_s
gubala's avatar
gubala committed
46 47 48 49 50 51 52 53
    begin
      GFTP::Exists.new.exists "/people/#{user.login}/", user.proxy do |test|
        logger.info "ZEUS DIR CHECK: #{test}"
        logger.info Time.now.to_s
        # TODO FIXME set zeus to true
      end
    rescue
      # TODO FIXME set zeus to false
gubala's avatar
gubala committed
54 55
    end

gubala's avatar
gubala committed
56 57 58 59 60 61 62 63 64 65 66
    logger.info Time.now.to_s
    begin
      GFTP::Exists.new.exists "gsiftp://#{Plgdata::Application.config.prometheus_gridftp_host}/net/people/#{user.login}/", user.proxy do |test|
        logger.info "PROM DIR CHECK: #{test}"
        logger.info Time.now.to_s
        # TODO FIXME set prom to true
      end
    rescue
      # TODO FIXME set prom to false
    end

gubala's avatar
gubala committed
67 68 69 70 71 72 73 74
    # logger.info Time.now.to_s
    # GFTP::Exists.new.exists "/people/#{user.login}/", user.proxy do |test|
    #   logger.info "ZEUS DIR CHECK: #{test}"
    #   logger.info Time.now.to_s
    #   # TODO FIXME
    # end

    # logger.info Time.now.to_s
gubala's avatar
gubala committed
75 76 77 78 79
    # GFTP::Exists.new.exists "gsiftp://#{Plgdata::Application.config.prometheus_gridftp_host}/net/people/#{user.login}/", user.proxy do |test|
    #   logger.info "PROM DIR CHECK: #{test}"
    #   logger.info Time.now.to_s
    #   # TODO FIXME
    # end
gubala's avatar
gubala committed
80

Tomasz Gubała's avatar
Tomasz Gubała committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    user.save

    user
  end


  # DK: utility function, which updates user_teams for the given user and raw array of user_team names returned by openid
  # each user team is created unless exists
  def self.update_user_teams(user, user_teams)
    user.user_teams = [] # remove old teams

    user_teams.each do |userteam|
      team_name, team_description = userteam.split('(')
      team_description.chop!

      Rails.logger.debug("Name: #{team_name}, Description: #{team_description}")

      team = UserTeam.where(shortname: team_name).first
      if team.nil?
        team = UserTeam.new(shortname: team_name, name: team_description)
      else
        team.shortname = team_name
        team.name = team_description
      end

      team.save

      user.user_teams << team
    end
  end

112 113 114 115
  # Requested by sec audit to explicitly remove the proxy cert from the DB on logout
  def clear_proxy
    update_attribute(:proxy, nil)
  end
Tomasz Gubała's avatar
Tomasz Gubała committed
116 117 118 119 120

  def full_name
    name + ' ' + surname
  end

121 122 123
  def beta?
    login == 'plggubala'
  end
Tomasz Gubała's avatar
Tomasz Gubała committed
124
end