lib/rack/oauth2/models/client.rb in rack-oauth2-server-1.2.2 vs lib/rack/oauth2/models/client.rb in rack-oauth2-server-1.3.0

- old
+ new

@@ -23,11 +23,12 @@ # them as suggested would result in better user experience. Don't ask # how we learned that. def create(args) redirect_uri = Server::Utils.parse_redirect_uri(args[:redirect_uri]).to_s if args[:redirect_uri] fields = { :secret=>Server.secure_random, :display_name=>args[:display_name], :link=>args[:link], - :image_url=>args[:image_url], :redirect_uri=>redirect_uri, :created_at=>Time.now.utc, :revoked=>nil } + :image_url=>args[:image_url], :redirect_uri=>redirect_uri, :created_at=>Time.now.utc.to_i, + :revoked=>nil } fields[:_id] = collection.insert(fields) Server.new_instance self, fields end # Lookup client by ID, display name or URL. @@ -36,10 +37,16 @@ Server.new_instance self, collection.find_one(id) rescue BSON::InvalidObjectId Server.new_instance self, collection.find_one({ :display_name=>field }) || collection.find_one({ :link=>field }) end + # Returns all the clients in the database, sorted alphabetically. + def all + collection.find({}, { :sort=>[[:display_name, Mongo::ASCENDING]] }). + map { |fields| Server.new_instance self, fields } + end + def collection Server.database["oauth2.clients"] end end @@ -63,14 +70,20 @@ attr_accessor :revoked # Revoke all authorization requests, access grants and access tokens for # this client. Ward off the evil. def revoke! - self.revoked = Time.now.utc + self.revoked = Time.now.utc.to_i Client.collection.update({ :_id=>id }, { :$set=>{ :revoked=>revoked } }) AuthRequest.collection.update({ :client_id=>id }, { :$set=>{ :revoked=>revoked } }) AccessGrant.collection.update({ :client_id=>id }, { :$set=>{ :revoked=>revoked } }) AccessToken.collection.update({ :client_id=>id }, { :$set=>{ :revoked=>revoked } }) + end + + def update(args) + fields = [:display_name, :link, :image_url].inject({}) { |h,k| v = args[k]; h[k] = v if v; h } + fields[:redirect_uri] = Server::Utils.parse_redirect_uri(args[:redirect_uri]).to_s if args[:redirect_uri] + self.class.collection.update({ :_id=>id }, { :$set=>fields }) end Server.create_indexes do # For quickly returning clients sorted by display name, or finding # client from a URL.