diff --git a/gears/carto_gears_api/lib/carto_gears_api/users_service.rb b/gears/carto_gears_api/lib/carto_gears_api/users_service.rb index 5fb69bfeef..f6a845149f 100644 --- a/gears/carto_gears_api/lib/carto_gears_api/users_service.rb +++ b/gears/carto_gears_api/lib/carto_gears_api/users_service.rb @@ -1,10 +1,38 @@ require 'values' +require 'uuidtools' module CartoGearsApi # User information. # - # @attr_reader [String] email Email - class User < Value.new(:email); end + # @attr_reader [UUID] id User id + # @attr_reader [String] username User name + # @attr_reader [String] email Email + # @attr_reader [CartoGearsApi::Organization] organization Organization + class User < Value.new(:id, :username, :email, :organization) + extend ActiveModel::Naming + include ActiveRecord::AttributeMethods::PrimaryKey + + def id + # This is needed to make ActiveRecord::AttributeMethods::PrimaryKey work. Otherwise it + # won't find the id accessible thanks to Value. Magic is not always compatible. + @id + end + + # @return [String] The subdomain required by this user in the current CARTO configuration. + # It takes into account subdomainless URLs and organizations. + def subdomain + if CartoDB.subdomainless_urls? + username + else + organization.nil? ? username : organization.name + end + end + end + + # Organization information. + # + # @attr_reader [String] name Organization name. + class Organization < Value.new(:name); end class UsersService # Returns the logged user at the request. @@ -17,8 +45,17 @@ module CartoGearsApi private - def user(user_model) - CartoGearsApi::User.with(email: user_model.email) + def user(user) + CartoGearsApi::User.with( + id: UUIDTools::UUID.parse(user.id), + username: user.username, + email: user.email, + organization: user.organization ? organization(user.organization) : nil + ) + end + + def organization(organization) + CartoGearsApi::Organization.with(name: organization.name) end end end