Sha256: 3dd8067ccf0b086b0ed010e2b55c27d748f7c7a4d8602d818d6add87efbdb380

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

class ProjectAccessDeterminator
  attr_reader :user
  attr_reader :project
  attr_reader :organization
  attr_reader :role

  def initialize(project: project, user: user, organization: nil)
    @user          = user
    @project       = project
    @organization  = organization
    @role          = determine_role
  end

  def access
    if organization
      organization_access_level
    else
      user_access_level
    end
  end

  def self.access(project: project, user: user, organization: nil)
    new(project: project, user: user, organization: organization).access
  end

  def determine_role
    @role = if organization
      organization.organization_users.find_by_user_id(user.id).try(:role)
    else
      project.project_users.find_by_user_id(user.id).try(:role)
    end
  end

  def organization_access_level
    role ? :admin : nil
  end

  def user_access_level
    if project.organizations.any?
      user_access_for_project_with_an_organization
    else
      user_access_for_project_without_an_organization
    end
  end

  def user_access_for_project_with_an_organization
    case role
    when "owner" then :member
    when "collaborator" then :collaborator
    else
      nil
    end
  end

  def user_access_for_project_without_an_organization
    case role
    when "owner" then :admin
    when "collaborator" then :collaborator
    else
      nil
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
self_systeem-0.1.0 test/dummy_app/app/services/project_access_determinator.rb