Commit 8a5ccf111b8f5af338ba3a46b0412b56fa77dd8b

Authored by Leandro Santos
2 parents 9003bd10 67f84ed1

Merge branch 'stable' of gitlab.com:participa/noosfero into stable

lib/api/api.rb
... ... @@ -1,38 +0,0 @@
1   -require 'grape'
2   -Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file}
3   -
4   -module API
5   - class API < Grape::API
6   - before { start_log }
7   - before { setup_multitenancy }
8   - before { detect_stuff_by_domain }
9   - after { end_log }
10   -
11   - version 'v1'
12   - prefix "api"
13   - format :json
14   - content_type :txt, "text/plain"
15   -
16   - helpers APIHelpers
17   -
18   - mount V1::Articles
19   - mount V1::Comments
20   - mount V1::Users
21   - mount V1::Communities
22   - mount V1::People
23   - mount V1::Enterprises
24   - mount V1::Categories
25   - mount Session
26   -
27   - # hook point which allow plugins to add Grape::API extensions to API::API
28   - #finds for plugins which has api mount points classes defined (the class should extends Grape::API)
29   - @plugins = Noosfero::Plugin.all.map { |p| p.constantize }
30   - @plugins.each do |klass|
31   - if klass.public_methods.include? 'api_mount_points'
32   - klass.api_mount_points.each do |mount_class|
33   - mount mount_class if mount_class && ( mount_class < Grape::API )
34   - end
35   - end
36   - end
37   - end
38   -end
lib/api/entities.rb
... ... @@ -1,87 +0,0 @@
1   -module API
2   - module Entities
3   -
4   - Grape::Entity.format_with :timestamp do |date|
5   - date.strftime('%Y/%m/%d %H:%M:%S') if date
6   - end
7   -
8   - class Image < Grape::Entity
9   - root 'images', 'image'
10   -
11   - expose :icon_url do |image, options|
12   - image.public_filename(:icon)
13   - end
14   -
15   - expose :minor_url do |image, options|
16   - image.public_filename(:minor)
17   - end
18   -
19   - expose :portrait_url do |image, options|
20   - image.public_filename(:portrait)
21   - end
22   -
23   - expose :thumb_url do |image, options|
24   - image.public_filename(:thumb)
25   - end
26   - end
27   -
28   - class Profile < Grape::Entity
29   - expose :identifier, :name, :id
30   - expose :created_at, :format_with => :timestamp
31   - expose :image, :using => Image
32   - end
33   -
34   - class Person < Profile;end;
35   - class Enterprise < Profile;end;
36   - class Community < Profile
37   - root 'communities', 'community'
38   - expose :description
39   - end
40   -
41   - class Category < Grape::Entity
42   - root 'categories', 'category'
43   - expose :name, :id, :slug
44   - expose :image, :using => Image
45   - end
46   -
47   -
48   - class Article < Grape::Entity
49   - root 'articles', 'article'
50   - expose :id, :body
51   - expose :created_at, :format_with => :timestamp
52   - expose :title, :documentation => {:type => "String", :desc => "Title of the article"}
53   - expose :created_by, :as => :author, :using => Profile
54   - expose :profile, :using => Profile
55   - expose :categories, :using => Category
56   - end
57   -
58   - class Comment < Grape::Entity
59   - root 'comments', 'comment'
60   - expose :body, :title, :id
61   - expose :created_at, :format_with => :timestamp
62   - expose :author, :using => Profile
63   - end
64   -
65   -
66   - class User < Grape::Entity
67   - root 'users', 'user'
68   - expose :id
69   - expose :login
70   - expose :person, :using => Profile
71   - expose :permissions do |user, options|
72   - output = {}
73   - user.person.role_assignments.map do |role_assigment|
74   - if role_assigment.resource.respond_to?(:identifier)
75   - output[role_assigment.resource.identifier] = role_assigment.role.permissions
76   - end
77   - end
78   - output
79   - end
80   - end
81   -
82   - class UserLogin < User
83   - expose :private_token
84   - end
85   -
86   - end
87   -end
lib/api/helpers.rb
... ... @@ -1,201 +0,0 @@
1   -module API
2   - module APIHelpers
3   - PRIVATE_TOKEN_PARAM = :private_token
4   -
5   - def logger
6   - @logger ||= Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_api.log"))
7   - end
8   -
9   - def current_user
10   - private_token = params[PRIVATE_TOKEN_PARAM].to_s
11   - @current_user ||= User.find_by_private_token(private_token)
12   - @current_user = nil if !@current_user.nil? && @current_user.private_token_expired?
13   - @current_user
14   - end
15   -
16   - def current_person
17   - current_user.person unless current_user.nil?
18   - end
19   -
20   - def logout
21   - @current_user = nil
22   - end
23   -
24   - def environment
25   - @environment
26   - end
27   -
28   - def limit
29   - limit = params[:limit].to_i
30   - limit = default_limit if limit <= 0
31   - limit
32   - end
33   -
34   - def period(from_date, until_date)
35   - begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date
36   - end_period = until_date.nil? ? DateTime.now : until_date
37   -
38   - begin_period...end_period
39   - end
40   -
41   - def parse_content_type(content_type)
42   - return nil if content_type.blank?
43   - content_type.split(',').map do |content_type|
44   - content_type.camelcase
45   - end
46   - end
47   -
48   - def make_conditions_with_parameter(params = {})
49   - conditions = {}
50   - from_date = DateTime.parse(params[:from]) if params[:from]
51   - until_date = DateTime.parse(params[:until]) if params[:until]
52   -
53   - conditions[:type] = parse_content_type(params[:content_type]) unless params[:content_type].nil?
54   -
55   - conditions[:created_at] = period(from_date, until_date) if from_date || until_date
56   -
57   - conditions
58   - end
59   -
60   -
61   - def select_filtered_collection_of(object, method, params)
62   - conditions = make_conditions_with_parameter(params)
63   -
64   - if params[:reference_id]
65   - objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
66   - else
67   - objects = object.send(method).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
68   - end
69   - objects
70   - end
71   -
72   -#FIXME see if its needed
73   -# def paginate(relation)
74   -# per_page = params[:per_page].to_i
75   -# paginated = relation.page(params[:page]).per(per_page)
76   -# add_pagination_headers(paginated, per_page)
77   -#
78   -# paginated
79   -# end
80   -
81   - def authenticate!
82   - unauthorized! unless current_user
83   - end
84   -
85   -#FIXME see if its needed
86   -# def authenticated_as_admin!
87   -# forbidden! unless current_user.is_admin?
88   -# end
89   -#
90   -#FIXME see if its needed
91   -# def authorize! action, subject
92   -# unless abilities.allowed?(current_user, action, subject)
93   -# forbidden!
94   -# end
95   -# end
96   -#
97   -#FIXME see if its needed
98   -# def can?(object, action, subject)
99   -# abilities.allowed?(object, action, subject)
100   -# end
101   -
102   - # Checks the occurrences of required attributes, each attribute must be present in the params hash
103   - # or a Bad Request error is invoked.
104   - #
105   - # Parameters:
106   - # keys (required) - A hash consisting of keys that must be present
107   - def required_attributes!(keys)
108   - keys.each do |key|
109   - bad_request!(key) unless params[key].present?
110   - end
111   - end
112   -
113   - # Checks the occurrences of uniqueness of attributes, each attribute must be present in the params hash
114   - # or a Bad Request error is invoked.
115   - #
116   - # Parameters:
117   - # keys (unique) - A hash consisting of keys that must be unique
118   - def unique_attributes!(obj, keys)
119   - keys.each do |key|
120   - cant_be_saved_request!(key) if obj.send("find_by_#{key.to_s}", params[key])
121   - end
122   - end
123   -
124   - def attributes_for_keys(keys)
125   - attrs = {}
126   - keys.each do |key|
127   - attrs[key] = params[key] if params[key].present? or (params.has_key?(key) and params[key] == false)
128   - end
129   - attrs
130   - end
131   -
132   - # error helpers
133   - def forbidden!
134   - render_api_error!('403 Forbidden', 403)
135   - end
136   -
137   - def cant_be_saved_request!(attribute)
138   - message = _("(Invalid request) #{attribute} can't be saved")
139   - render_api_error!(message, 400)
140   - end
141   -
142   - def bad_request!(attribute)
143   - message = _("(Bad request) #{attribute} not given")
144   - render_api_error!(message, 400)
145   - end
146   -
147   - def something_wrong!
148   - message = _("Something wrong happened")
149   - render_api_error!(message, 400)
150   - end
151   -
152   - def unauthorized!
153   - render_api_error!(_('Unauthorized'), 401)
154   - end
155   -
156   - def not_allowed!
157   - render_api_error!(_('Method Not Allowed'), 405)
158   - end
159   -
160   - def render_api_error!(message, status)
161   - error!({'message' => message, :code => status}, status)
162   - end
163   -
164   - def render_api_errors!(messages)
165   - render_api_error!(messages.join(','), 400)
166   - end
167   - protected
168   -
169   - def start_log
170   - logger.info "Started #{request.path} #{request.params.except('password')}"
171   - end
172   - def end_log
173   - logger.info "Completed #{request.path}"
174   - end
175   -
176   - def setup_multitenancy
177   - Noosfero::MultiTenancy.setup!(request.host)
178   - end
179   -
180   - def detect_stuff_by_domain
181   - @domain = Domain.find_by_name(request.host)
182   - if @domain.nil?
183   - @environment = Environment.default
184   - if @environment.nil? && Rails.env.development?
185   - # This should only happen in development ...
186   - @environment = Environment.create!(:name => "Noosfero", :is_default => true)
187   - end
188   - else
189   - @environment = @domain.environment
190   - end
191   - end
192   -
193   - private
194   -
195   - def default_limit
196   - 20
197   - end
198   -
199   -
200   - end
201   -end
lib/api/session.rb
... ... @@ -1,44 +0,0 @@
1   -module API
2   -
3   - class Session < Grape::API
4   -
5   - # Login to get token
6   - #
7   - # Parameters:
8   - # login (*required) - user login or email
9   - # password (required) - user password
10   - #
11   - # Example Request:
12   - # POST /login?login=some&password=pass
13   - post "/login" do
14   - user ||= User.authenticate(params[:login], params[:password], environment)
15   -
16   - return unauthorized! unless user
17   - user.generate_private_token!
18   - present user, :with => Entities::UserLogin
19   - end
20   -
21   - # Create user.
22   - #
23   - # Parameters:
24   - # email (required) - Email
25   - # password (required) - Password
26   - # login - login
27   - # Example Request:
28   - # POST /register?email=some@mail.com&password=pas&login=some
29   - post "/register" do
30   - required_attributes! [:email, :login, :password]
31   - unique_attributes! User, [:email, :login]
32   - attrs = attributes_for_keys [:email, :login, :password]
33   - attrs[:password_confirmation] = attrs[:password]
34   - user = User.new(attrs)
35   - if user.save
36   - user.activate
37   - present user, :with => Entities::User
38   - else
39   - something_wrong!
40   - end
41   - end
42   -
43   - end
44   -end
lib/api/v1/articles.rb
... ... @@ -1,85 +0,0 @@
1   -module API
2   - module V1
3   - class Articles < Grape::API
4   - before { authenticate! }
5   -
6   - resource :articles do
7   -
8   - # Collect comments from articles
9   - #
10   - # Parameters:
11   - # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created
12   - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
13   - # limit - amount of comments returned. The default value is 20
14   - #
15   - # Example Request:
16   - # GET /api/v1/articles?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10&content_type=Hub
17   -# desc 'Articles.', {
18   -# :params => API::Entities::Article.documentation
19   -# }
20   - get do
21   - articles = select_filtered_collection_of(environment, 'articles', params)
22   - present articles, :with => Entities::Article
23   - end
24   -
25   - desc "Return the article id"
26   - get ':id' do
27   - present environment.articles.find(params[:id]), :with => Entities::Article
28   - end
29   -
30   - get ':id/children' do
31   -
32   - conditions = make_conditions_with_parameter(params)
33   - if params[:reference_id]
34   - articles = environment.articles.find(params[:id]).children.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
35   - else
36   - articles = environment.articles.find(params[:id]).children.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
37   - end
38   - present articles, :with => Entities::Article
39   - end
40   -
41   - get ':id/children/:child_id' do
42   - present environment.articles.find(params[:id]).children.find(params[:child_id]), :with => Entities::Article
43   - end
44   -
45   -
46   - end
47   -
48   - resource :communities do
49   - segment '/:community_id' do
50   - resource :articles do
51   - get do
52   - community = environment.communities.find(params[:community_id])
53   - articles = select_filtered_collection_of(community, 'articles', params)
54   - present articles, :with => Entities::Article
55   - end
56   -
57   - get '/:id' do
58   - community = environment.communities.find(params[:community_id])
59   - present community.articles.find(params[:id]), :with => Entities::Article
60   - end
61   -
62   - # Example Request:
63   - # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body
64   - post do
65   - community = environment.communities.find(params[:community_id])
66   - klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]
67   - article = klass_type.constantize.new(params[:article])
68   - article.last_changed_by = current_person
69   - article.created_by= current_person
70   - article.profile = community
71   -
72   - if !article.save
73   - render_api_errors!(article.errors.full_messages)
74   - end
75   - present article, :with => Entities::Article
76   - end
77   -
78   - end
79   - end
80   -
81   - end
82   -
83   - end
84   - end
85   -end
lib/api/v1/categories.rb
... ... @@ -1,23 +0,0 @@
1   -module API
2   - module V1
3   - class Categories < Grape::API
4   - before { authenticate! }
5   -
6   - resource :categories do
7   -
8   - get do
9   - type = params[:category_type]
10   - categories = type.nil? ? environment.categories : environment.categories.find(:all, :conditions => {:type => type})
11   - present categories, :with => Entities::Category
12   - end
13   -
14   - desc "Return the category by id"
15   - get ':id' do
16   - present environment.categories.find(params[:id]), :with => Entities::Category
17   - end
18   -
19   - end
20   -
21   - end
22   - end
23   -end
lib/api/v1/comments.rb
... ... @@ -1,42 +0,0 @@
1   -module API
2   - module V1
3   - class Comments < Grape::API
4   - before { authenticate! }
5   -
6   - resource :articles do
7   - # Collect comments from articles
8   - #
9   - # Parameters:
10   - # reference_id - comment id used as reference to collect comment
11   - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
12   - # limit - amount of comments returned. The default value is 20
13   - #
14   - # Example Request:
15   - # GET /articles/12/comments?oldest&limit=10&reference_id=23
16   - get ":id/comments" do
17   -
18   - conditions = make_conditions_with_parameter(params)
19   -
20   - if params[:reference_id]
21   - comments = environment.articles.find(params[:id]).comments.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
22   - else
23   - comments = environment.articles.find(params[:id]).comments.reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
24   - end
25   - present comments, :with => Entities::Comment
26   -
27   - end
28   -
29   - get ":id/comments/:comment_id" do
30   - present environment.articles.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::Comment
31   - end
32   -
33   - # Example Request:
34   - # POST api/v1/articles/12/comments?private_toke=234298743290432&body=new comment
35   - post ":id/comments" do
36   - present environment.articles.find(params[:id]).comments.create(:author => current_person, :body => params[:body]), :with => Entities::Comment
37   - end
38   - end
39   -
40   - end
41   - end
42   -end
lib/api/v1/communities.rb
... ... @@ -1,38 +0,0 @@
1   -module API
2   - module V1
3   - class Communities < Grape::API
4   - before { authenticate! }
5   -
6   - resource :communities do
7   -
8   - # Collect comments from articles
9   - #
10   - # Parameters:
11   - # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created
12   - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
13   - # limit - amount of comments returned. The default value is 20
14   - #
15   - # Example Request:
16   - # GET /communities?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10
17   - # GET /communities?reference_id=10&limit=10&oldest
18   - get do
19   - communities = select_filtered_collection_of(current_person, 'communities', params)
20   - present communities, :with => Entities::Community
21   - end
22   -
23   - #FIXME See only public communities
24   - get '/all' do
25   - communities = select_filtered_collection_of(environment, 'communities', params)
26   - present communities, :with => Entities::Community
27   - end
28   -
29   - get ':id' do
30   - community = environment.communities.find(params[:id])
31   - present community, :with => Entities::Community
32   - end
33   -
34   - end
35   -
36   - end
37   - end
38   -end
lib/api/v1/enterprises.rb
... ... @@ -1,32 +0,0 @@
1   -module API
2   - module V1
3   - class Enterprises < Grape::API
4   - before { authenticate! }
5   -
6   - resource :enterprises do
7   -
8   - # Collect comments from articles
9   - #
10   - # Parameters:
11   - # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created
12   - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
13   - # limit - amount of comments returned. The default value is 20
14   - #
15   - # Example Request:
16   - # GET /enterprises?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10
17   - # GET /enterprises?reference_id=10&limit=10&oldest
18   - get do
19   - enterprises = select_filtered_collection_of(environment, 'enterprises', params)
20   - present enterprises, :with => Entities::Enterprise
21   - end
22   -
23   - desc "Return the article id"
24   - get ':id' do
25   - present environment.enterprises.find(params[:id]), :with => Entities::Enterprise
26   - end
27   -
28   - end
29   -
30   - end
31   - end
32   -end
lib/api/v1/people.rb
... ... @@ -1,32 +0,0 @@
1   -module API
2   - module V1
3   - class People < Grape::API
4   - before { authenticate! }
5   -
6   - resource :people do
7   -
8   - # Collect comments from articles
9   - #
10   - # Parameters:
11   - # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created
12   - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
13   - # limit - amount of comments returned. The default value is 20
14   - #
15   - # Example Request:
16   - # GET /people?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10
17   - # GET /people?reference_id=10&limit=10&oldest
18   - get do
19   - people = select_filtered_collection_of(environment, 'people', params)
20   - present people, :with => Entities::Person
21   - end
22   -
23   - desc "Return the person information"
24   - get '/:id' do
25   - present environment.people.find(params[:id]), :with => Entities::Person
26   - end
27   -
28   - end
29   -
30   - end
31   - end
32   -end
lib/api/v1/users.rb
... ... @@ -1,31 +0,0 @@
1   -module API
2   - module V1
3   - class Users < Grape::API
4   - before { authenticate! }
5   -
6   - resource :users do
7   -
8   - get do
9   - present environment.users, :with => Entities::User
10   - end
11   -
12   - get ":id" do
13   - present environment.users.find(params[:id]), :with => Entities::User
14   - end
15   -
16   - get ":id/permissions" do
17   - user = environment.users.find(params[:id])
18   - output = {}
19   - user.person.role_assignments.map do |role_assigment|
20   - if role_assigment.resource.respond_to?(:identifier) && role_assigment.resource.identifier == params[:profile]
21   - output[:permissions] = role_assigment.role.permissions
22   - end
23   - end
24   - present output
25   - end
26   -
27   - end
28   -
29   - end
30   - end
31   -end