Commit 8856c65fa8e7b34e81f3aa2dd0edd3e8d1b3e71b
1 parent
6bc2576f
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
add user registration
Showing
3 changed files
with
64 additions
and
99 deletions
Show diff stats
lib/api/helpers.rb
| @@ -13,7 +13,7 @@ module API | @@ -13,7 +13,7 @@ module API | ||
| 13 | @current_user = nil | 13 | @current_user = nil |
| 14 | end | 14 | end |
| 15 | 15 | ||
| 16 | - | 16 | +#FIXME see if its needed |
| 17 | # def paginate(relation) | 17 | # def paginate(relation) |
| 18 | # per_page = params[:per_page].to_i | 18 | # per_page = params[:per_page].to_i |
| 19 | # paginated = relation.page(params[:page]).per(per_page) | 19 | # paginated = relation.page(params[:page]).per(per_page) |
| @@ -26,16 +26,19 @@ module API | @@ -26,16 +26,19 @@ module API | ||
| 26 | unauthorized! unless current_user | 26 | unauthorized! unless current_user |
| 27 | end | 27 | end |
| 28 | 28 | ||
| 29 | +#FIXME see if its needed | ||
| 29 | # def authenticated_as_admin! | 30 | # def authenticated_as_admin! |
| 30 | # forbidden! unless current_user.is_admin? | 31 | # forbidden! unless current_user.is_admin? |
| 31 | # end | 32 | # end |
| 32 | # | 33 | # |
| 34 | +#FIXME see if its needed | ||
| 33 | # def authorize! action, subject | 35 | # def authorize! action, subject |
| 34 | # unless abilities.allowed?(current_user, action, subject) | 36 | # unless abilities.allowed?(current_user, action, subject) |
| 35 | # forbidden! | 37 | # forbidden! |
| 36 | # end | 38 | # end |
| 37 | # end | 39 | # end |
| 38 | # | 40 | # |
| 41 | +#FIXME see if its needed | ||
| 39 | # def can?(object, action, subject) | 42 | # def can?(object, action, subject) |
| 40 | # abilities.allowed?(object, action, subject) | 43 | # abilities.allowed?(object, action, subject) |
| 41 | # end | 44 | # end |
| @@ -51,6 +54,17 @@ module API | @@ -51,6 +54,17 @@ module API | ||
| 51 | end | 54 | end |
| 52 | end | 55 | end |
| 53 | 56 | ||
| 57 | + # Checks the occurrences of uniqueness of attributes, each attribute must be present in the params hash | ||
| 58 | + # or a Bad Request error is invoked. | ||
| 59 | + # | ||
| 60 | + # Parameters: | ||
| 61 | + # keys (unique) - A hash consisting of keys that must be unique | ||
| 62 | + def unique_attributes!(obj, keys) | ||
| 63 | + keys.each do |key| | ||
| 64 | + cant_be_saved_request!(key) if obj.send("find_by_#{key.to_s}", params[key]) | ||
| 65 | + end | ||
| 66 | + end | ||
| 67 | + | ||
| 54 | def attributes_for_keys(keys) | 68 | def attributes_for_keys(keys) |
| 55 | attrs = {} | 69 | attrs = {} |
| 56 | keys.each do |key| | 70 | keys.each do |key| |
| @@ -60,49 +74,36 @@ module API | @@ -60,49 +74,36 @@ module API | ||
| 60 | end | 74 | end |
| 61 | 75 | ||
| 62 | # error helpers | 76 | # error helpers |
| 63 | - | ||
| 64 | def forbidden! | 77 | def forbidden! |
| 65 | render_api_error!('403 Forbidden', 403) | 78 | render_api_error!('403 Forbidden', 403) |
| 66 | end | 79 | end |
| 67 | 80 | ||
| 81 | + def cant_be_saved_request!(attribute) | ||
| 82 | + message = _("(Invalid request) #{attribute} can't be saved") | ||
| 83 | + render_api_error!(message, 400) | ||
| 84 | + end | ||
| 85 | + | ||
| 68 | def bad_request!(attribute) | 86 | def bad_request!(attribute) |
| 69 | - message = ["400 (Bad request)"] | ||
| 70 | - message << "\"" + attribute.to_s + "\" not given" | ||
| 71 | - render_api_error!(message.join(' '), 400) | 87 | + message = _("(Bad request) #{attribute} not given") |
| 88 | + render_api_error!(message, 400) | ||
| 72 | end | 89 | end |
| 73 | 90 | ||
| 74 | - def not_found!(resource = nil) | ||
| 75 | - message = ["404"] | ||
| 76 | - message << resource if resource | ||
| 77 | - message << "Not Found" | ||
| 78 | - render_api_error!(message.join(' '), 404) | 91 | + def something_wrong! |
| 92 | + message = _("Something wrong happened") | ||
| 93 | + render_api_error!(message, 400) | ||
| 79 | end | 94 | end |
| 80 | 95 | ||
| 81 | def unauthorized! | 96 | def unauthorized! |
| 82 | - render_api_error!('401 Unauthorized', 401) | 97 | + render_api_error!(_('Unauthorized'), 401) |
| 83 | end | 98 | end |
| 84 | 99 | ||
| 85 | def not_allowed! | 100 | def not_allowed! |
| 86 | - render_api_error!('Method Not Allowed', 405) | 101 | + render_api_error!(_('Method Not Allowed'), 405) |
| 87 | end | 102 | end |
| 88 | 103 | ||
| 89 | def render_api_error!(message, status) | 104 | def render_api_error!(message, status) |
| 90 | - error!({'message' => message}, status) | 105 | + error!({'message' => message, :code => status}, status) |
| 91 | end | 106 | end |
| 92 | 107 | ||
| 93 | -# private | ||
| 94 | -# | ||
| 95 | -# def add_pagination_headers(paginated, per_page) | ||
| 96 | -# request_url = request.url.split('?').first | ||
| 97 | -# | ||
| 98 | -# links = [] | ||
| 99 | -# links << %(<#{request_url}?page=#{paginated.current_page - 1}&per_page=#{per_page}>; rel="prev") unless paginated.first_page? | ||
| 100 | -# links << %(<#{request_url}?page=#{paginated.current_page + 1}&per_page=#{per_page}>; rel="next") unless paginated.last_page? | ||
| 101 | -# links << %(<#{request_url}?page=1&per_page=#{per_page}>; rel="first") | ||
| 102 | -# links << %(<#{request_url}?page=#{paginated.total_pages}&per_page=#{per_page}>; rel="last") | ||
| 103 | -# | ||
| 104 | -# header 'Link', links.join(', ') | ||
| 105 | -# end | ||
| 106 | - | ||
| 107 | end | 108 | end |
| 108 | end | 109 | end |
lib/api/session.rb
| 1 | module API | 1 | module API |
| 2 | 2 | ||
| 3 | -# require 'api/validations/uniqueness' | ||
| 4 | - | ||
| 5 | - # Users API | ||
| 6 | class Session < Grape::API | 3 | class Session < Grape::API |
| 7 | -#params do | ||
| 8 | -# requires :login, :uniqueness => true | ||
| 9 | -#end | ||
| 10 | 4 | ||
| 11 | # Login to get token | 5 | # Login to get token |
| 12 | # | 6 | # |
| @@ -15,9 +9,8 @@ module API | @@ -15,9 +9,8 @@ module API | ||
| 15 | # password (required) - user password | 9 | # password (required) - user password |
| 16 | # | 10 | # |
| 17 | # Example Request: | 11 | # Example Request: |
| 18 | - # POST /session | 12 | + # POST /login?login=some&password=pass |
| 19 | get "/login" do | 13 | get "/login" do |
| 20 | -# post "/session" do | ||
| 21 | environment = nil #FIXME load the correct environment create a method in helper | 14 | environment = nil #FIXME load the correct environment create a method in helper |
| 22 | user ||= User.authenticate(params[:login], params[:password], environment) | 15 | user ||= User.authenticate(params[:login], params[:password], environment) |
| 23 | 16 | ||
| @@ -26,35 +19,26 @@ environment = nil #FIXME load the correct environment create a method in helper | @@ -26,35 +19,26 @@ environment = nil #FIXME load the correct environment create a method in helper | ||
| 26 | present user, :with => Entities::UserLogin | 19 | present user, :with => Entities::UserLogin |
| 27 | end | 20 | end |
| 28 | 21 | ||
| 29 | - # Create user. | ||
| 30 | - # | ||
| 31 | - # Parameters: | ||
| 32 | - # email (required) - Email | ||
| 33 | - # password (required) - Password | ||
| 34 | - # name - Name | ||
| 35 | - # Example Request: | ||
| 36 | - # POST /users | ||
| 37 | -# post do | ||
| 38 | - get "register" do | ||
| 39 | - required_attributes! [:email, :login, :password] | ||
| 40 | - attrs = attributes_for_keys [:email, :login, :password] | ||
| 41 | - attrs[:password_confirmation] = attrs[:password] | ||
| 42 | - user = User.new(attrs) | ||
| 43 | -begin | ||
| 44 | - if user.save | ||
| 45 | - present user, :with => Entities::User | ||
| 46 | - else | ||
| 47 | - not_found! | ||
| 48 | - end | ||
| 49 | -rescue | ||
| 50 | -# not_found! | ||
| 51 | -#FIXME See why notfound is not working | ||
| 52 | -{} | ||
| 53 | -end | ||
| 54 | -# user | 22 | + # Create user. |
| 23 | + # | ||
| 24 | + # Parameters: | ||
| 25 | + # email (required) - Email | ||
| 26 | + # password (required) - Password | ||
| 27 | + # login - login | ||
| 28 | + # Example Request: | ||
| 29 | + # POST /register?email=some@mail.com&password=pas&login=some | ||
| 30 | + post "register" do | ||
| 31 | + required_attributes! [:email, :login, :password] | ||
| 32 | + unique_attributes! User, [:email, :login] | ||
| 33 | + attrs = attributes_for_keys [:email, :login, :password] | ||
| 34 | + attrs[:password_confirmation] = attrs[:password] | ||
| 35 | + user = User.new(attrs) | ||
| 36 | + if user.save | ||
| 37 | + present user, :with => Entities::User | ||
| 38 | + else | ||
| 39 | + something_wrong! | ||
| 55 | end | 40 | end |
| 56 | - | ||
| 57 | - | 41 | + end |
| 58 | 42 | ||
| 59 | end | 43 | end |
| 60 | end | 44 | end |
lib/api/v1/users.rb
| 1 | module API | 1 | module API |
| 2 | module V1 | 2 | module V1 |
| 3 | - class Users < Grape::API | ||
| 4 | - | ||
| 5 | - before { authenticate! } | ||
| 6 | - | ||
| 7 | - resource :users do | ||
| 8 | - | ||
| 9 | - #FIXME make the pagination | ||
| 10 | - #FIXME put it on environment context | ||
| 11 | -# get do | ||
| 12 | -# Users.all | ||
| 13 | -# end | ||
| 14 | - | ||
| 15 | - get ":id" do | ||
| 16 | - present Article.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::User | ||
| 17 | - end | ||
| 18 | - | ||
| 19 | - # Create user. | ||
| 20 | - # | ||
| 21 | - # Parameters: | ||
| 22 | - # email (required) - Email | ||
| 23 | - # password (required) - Password | ||
| 24 | - # name - Name | ||
| 25 | - # Example Request: | ||
| 26 | - # POST /users | ||
| 27 | -# post do | ||
| 28 | - get do | ||
| 29 | -# authenticated_as_admin! | ||
| 30 | - required_attributes! [:email, :login, :password] | ||
| 31 | - attrs = attributes_for_keys [:email, :login, :password] | ||
| 32 | - user = User.new(attrs) | ||
| 33 | - if user.save | ||
| 34 | - present user, :with => Entities::User | ||
| 35 | - else | ||
| 36 | - not_found! | 3 | + class Users < Grape::API |
| 4 | + | ||
| 5 | + before { authenticate! } | ||
| 6 | + | ||
| 7 | + resource :users do | ||
| 8 | + | ||
| 9 | + #FIXME make the pagination | ||
| 10 | + #FIXME put it on environment context | ||
| 11 | + get do | ||
| 12 | + Users.all | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + get ":id" do | ||
| 16 | + present Article.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::User | ||
| 37 | end | 17 | end |
| 18 | + | ||
| 38 | end | 19 | end |
| 20 | + | ||
| 39 | end | 21 | end |
| 40 | - | ||
| 41 | - end | ||
| 42 | end | 22 | end |
| 43 | end | 23 | end |