33 lines
711 B
Ruby
33 lines
711 B
Ruby
class RemoteStorageAuthorization < ApplicationRecord
|
|
belongs_to :user
|
|
|
|
serialize :permissions
|
|
|
|
validates_presence_of :permissions
|
|
validates_presence_of :client_id
|
|
|
|
scope :valid, -> { where(expire_at: nil).or(where(expire_at: (DateTime.now)..)) }
|
|
scope :expired, -> { where(expire_at: ..(DateTime.now)) }
|
|
|
|
after_initialize do |a|
|
|
a.permisisons = [] if a.permissions == nil
|
|
end
|
|
|
|
before_create :generate_token
|
|
|
|
def url
|
|
if self.redirect_uri
|
|
uri = URI.parse self.redirect_uri
|
|
"#{uri.scheme}://#{client_id}"
|
|
else
|
|
"http://#{client_id}"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def generate_token(length=16)
|
|
self.token = SecureRandom.hex(length) if self.token.blank?
|
|
end
|
|
end
|