docbrown/app/controllers/api/v0/articles_controller.rb
Andy Zhao 47d9ec27fb Allow users to belong to multiple orgs (#2583)
* Allow user to have many orgs

* Allow users to handle multi orgs in settings

* Make rounded buttons inline

* Add multi org function to dashboards

* Fix merge conflicts

* Fix mistake in merge conflict fix oops

* Display the correct membership level

* Fix accessibility issues

* Display organizations for article editors

* Handle submitting org id with preact editors

* Make listings work with multiple organizations

* Allow listings to have multiple orgs on create

* Display the correct number of credits for each org

* Move script tag to Webpack

* Allow multi orgs for purchasing and viewing credits

* Use OrganizationMembership as authorization check

* Display multiple organizations for notifications

* Allow dashboard to be viewable under multi-orgs

* Remove unused method

* Add multi-org functionality for article editors

* Show pro dashboard buttons for member+ org levels

* Leave the correct organization

* Allow article API to change org id

* Add left-out authorization method oops

* Make nav buttons a bit more clear

* Fix merge conflict

* Fix adding org id for /api/articles and tests

* Fix tests for org policy

* Use proper logic for displaying org members

* Update org actions with new authorization

* Use correct org when creating a listing

* Remove additional payment charge oops

* Mark org notifications as read with authorization

* Remove deprecated post_as_organization attribute

* Use new org_admin syntax

* Remove deprecated org logic for article create and update

* Default all RSS posts to not belong to any org

* Render org_member page for guest users

* Update org policy spec to work with multi orgs

* Use org_membership for org traits and move identity code

* Use org_member trait

* Update to work with multi-orgs

* Validate article's org_id if param org_id is blank

* Make  a let variable

* Remove unnecessary eager load for credits

* Fix HTML structure and org logic for non-org users

* Update credits spec for multi-org

* Add test for failed payment when purchased by org

* Lint listings_spec

* Test that the listing was created under the user

* Add tests for POST /listings multi-org

* Use double quotes for classes

* Fix /manage and a few other multi-org bugs

* Fix test for multi org

* Use correct method SQL exists? not Rails exist?

* Fix reads spec for multi-org

* Fix org_controller actions to work with multi org

* Test only multi org and not old usage and fix leave_org

* Fix org showing user profile img test for multi-org

* Fix org logic for users with no orgs

* Remove switch org functionality

* Update tests and add hidden param for org id

* Redirect to the specific organization

* Test other org button actions

* Use settings_notice instead of legacy notice and refactor

* Fix weird extra end issue prob from merge conflicts

* Test for with new flash key

* Fix user_views_org tests for multi-org

* Test for new flash message

* Update snapshot with new a11y html

* Move styling to stylesheet

* Add site admins functionality

* Move org_member? method in user model and refactor

* Use unspent_credits_count for organizations

* Add tests for /listings/new and minor bug fixes

* Use .present? in case of empty array

* Fix a lingering deprecated method

* Use greater than 1 for random numbers

* Add tests for counting spent and unspent credits
2019-06-04 09:30:52 -04:00

93 lines
3 KiB
Ruby

module Api
module V0
class ArticlesController < ApiController
respond_to :json
before_action :authenticate_with_api_key, only: %i[create update]
before_action :set_cache_control_headers, only: [:index]
caches_action :show,
cache_path: proc { |c| c.params.permit! },
expires_in: 5.minutes
before_action :cors_preflight_check
after_action :cors_set_access_control_headers
# skip CSRF checks for create and update
skip_before_action :verify_authenticity_token, only: %i[create update]
def index
@articles = ArticleApiIndexService.new(params).get
key_headers = [
"articles_api",
params[:tag],
params[:page],
params[:username],
params[:signature],
params[:state],
]
set_surrogate_key_header key_headers.join("_")
end
def show
relation = Article.published.includes(:user)
@article = if params[:id] == "by_path"
relation.find_by!(path: params[:url]).decorate
else
relation.find(params[:id]).decorate
end
end
def onboarding
tag_list = if params[:tag_list].present?
params[:tag_list].split(",")
else
%w[career discuss productivity]
end
@articles = []
4.times do
@articles << Suggester::Articles::Classic.new.get(tag_list)
end
Article.tagged_with(tag_list, any: true).
order("published_at DESC").
where("positive_reactions_count > ? OR comments_count > ? AND published = ?", 10, 3, true).
limit(15).each do |article|
@articles << article
end
@articles = @articles.uniq.sample(6)
end
def create
@article = ArticleCreationService.new(@user, article_params).create!
render "show", status: :created, location: @article.url
end
def update
@article = Articles::Updater.call(@user, params[:id], article_params)
render "show", status: :ok
end
private
def article_params
allowed_params = [
:title, :body_markdown, :published, :series,
:main_image, :canonical_url, :description, tags: []
]
allowed_params << :organization_id if params["article"]["organization_id"] && allowed_to_change_org_id?
params.require(:article).permit(allowed_params)
end
def allowed_to_change_org_id?
potential_user = @article&.user || @user
if @article.nil? || OrganizationMembership.exists?(user: potential_user, organization_id: params["article"]["organization_id"])
OrganizationMembership.exists?(user: potential_user, organization_id: params["article"]["organization_id"])
elsif potential_user == @user
potential_user.org_admin?(params["article"]["organization_id"]) ||
@user.any_admin?
end
end
end
end
end