Module: Sufia::User

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/sufia/user.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#abilityObject



128
129
130
# File 'app/models/concerns/sufia/user.rb', line 128

def ability
  @ability ||= ::Ability.new(self)
end

#all_user_activity(since = DateTime.now.to_i - 8640) ⇒ Object



132
133
134
135
136
# File 'app/models/concerns/sufia/user.rb', line 132

def all_user_activity(since = DateTime.now.to_i - 8640)
  events = self.events.reverse.collect { |event| event if event[:timestamp].to_i > since }.compact
  profile_events = self.profile_events.reverse.collect { |event| event if event[:timestamp].to_i > since }.compact
  events.concat(profile_events).sort { |a, b| b[:timestamp].to_i <=> a[:timestamp].to_i }
end

#as_json(_opts = nil) ⇒ Object

Format the json for select2 which requires just an id and a field called text. If we need an alternate format we should probably look at a json template gem



82
83
84
# File 'app/models/concerns/sufia/user.rb', line 82

def as_json(_opts = nil)
  { id: user_key, text: display_name ? "#{display_name} (#{user_key})" : user_key }
end

#email_addressObject



91
92
93
# File 'app/models/concerns/sufia/user.rb', line 91

def email_address
  email
end

#groupsObject

The basic groups method, override or will fallback to Sufia::Ldap::User



124
125
126
# File 'app/models/concerns/sufia/user.rb', line 124

def groups
  @groups ||= group_list ? group_list.split(";?;") : []
end

#mailboxer_email(_obj = nil) ⇒ Object

method needed for messaging



119
120
121
# File 'app/models/concerns/sufia/user.rb', line 119

def mailboxer_email(_obj = nil)
  nil
end

#nameObject



95
96
97
98
99
# File 'app/models/concerns/sufia/user.rb', line 95

def name
  display_name.titleize || raise
rescue
  user_key
end

#normalize_orcidObject

Coerce the ORCID into URL format



70
71
72
73
74
75
76
77
78
# File 'app/models/concerns/sufia/user.rb', line 70

def normalize_orcid
  # Skip normalization if:
  #   1. validation has already flagged the ORCID as invalid
  #   2. the orcid field is blank
  #   3. the orcid is already in its normalized form
  return if errors[:orcid].first.present? || orcid.blank? || orcid.starts_with?('http://orcid.org/')
  bare_orcid = Sufia::OrcidValidator.match(orcid).string
  self.orcid = "http://orcid.org/#{bare_orcid}"
end

#populate_attributesObject

Populate user instance with attributes from remote system (e.g., LDAP) There is no default implementation – override this in your application



88
89
# File 'app/models/concerns/sufia/user.rb', line 88

def populate_attributes
end

#set_arkivo_tokenObject



58
59
60
# File 'app/models/concerns/sufia/user.rb', line 58

def set_arkivo_token
  self.arkivo_token ||= token_algorithm
end

#to_paramObject

Redefine this for more intuitive keys in Redis



102
103
104
105
# File 'app/models/concerns/sufia/user.rb', line 102

def to_param
  # HACK: because rails doesn't like periods in urls.
  user_key.gsub(/\./, '-dot-')
end

#token_algorithmObject



62
63
64
65
66
67
# File 'app/models/concerns/sufia/user.rb', line 62

def token_algorithm
  loop do
    token = SecureRandom.base64(24)
    return token if User.find_by(arkivo_token: token).nil?
  end
end

#trophy_filesObject



107
108
109
110
111
112
113
114
115
116
# File 'app/models/concerns/sufia/user.rb', line 107

def trophy_files
  trophies.map do |t|
    begin
      ::GenericFile.load_instance_from_solr(t.generic_file_id)
    rescue ActiveFedora::ObjectNotFoundError
      logger.error("Invalid trophy for user #{user_key} (generic file id #{t.generic_file_id})")
      nil
    end
  end.compact
end

#zotero_tokenObject



45
46
47
# File 'app/models/concerns/sufia/user.rb', line 45

def zotero_token
  self[:zotero_token].blank? ? nil : Marshal.load(self[:zotero_token])
end

#zotero_token=(value) ⇒ Object



49
50
51
52
53
54
55
56
# File 'app/models/concerns/sufia/user.rb', line 49

def zotero_token=(value)
  self[:zotero_token] = if value.blank?
                          # Resetting the token
                          value
                        else
                          Marshal.dump(value)
                        end
end