<commit_before>class Admin::UsersController < Admin::ApplicationController

  def index
    @users = User.all
  end

  def edit
    @user = User.find(params[:id])
    @profiles = Profile.all.collect {|b| [ b.desc, b.id ] }
  end

  def update
    @user = User.find(params[:id])
    @user.update_attributes(params[:user])
    if @user.save
      redirect_to admin_users_path, :notice => "Usuário atualizado."
    else
      render :edit
    end
  end

  def books
    @user = User.find(params[:id])
    @books = Book.where("organizer_id != #{@user.id}")
    @user_books = []
    @user_books = @user_books.concat(@user.books).flatten
    @books = @books - @user_books
  end

  def add_book
    @user = User.find(params[:id])
    @book = Book.find(params[:user][:book_id])

    @user.books << @book

    if @user.save
      redirect_to admin_users_path, :notice => "Livro foi adicionado para o usuário."
    else
      render :books
    end
  end

  def remove_book
    @user = User.find(params[:id])
    @book = Book.find(params[:book_id])

    @user.books.delete(@book)

    if @user.save
      redirect_to admin_users_path, :notice => "Foi removido o acesso do livro para o usuário."
    else
      render :books
    end
  end

end<commit_msg>2.0.2 #11 https://trello.com/c/Zk1QYkRb  admin and publisher<commit_after>class Admin::UsersController < Admin::ApplicationController

  skip_before_filter :authentication_admin_check, only: [:index, :books, :add_book, :remove_book]
  before_filter :auth_admin_or_publisher, only: [:index, :books, :add_book, :remove_book]

  def index
    if current_user.admin?
      @users = User.all
    elsif current_user.publisher?
      @users = User.joins(:books).where('books.id in (?)', current_user.organized_books).group('users.id')
    end      
  end

  def edit
    @user = User.find(params[:id])
    @profiles = Profile.all.collect {|b| [ b.desc, b.id ] }
  end

  def update
    @user = User.find(params[:id])
    @user.update_attributes(params[:user])
    if @user.save
      redirect_to admin_users_path, :notice => "Usuário atualizado."
    else
      render :edit
    end
  end

  def books
    @user = User.find(params[:id])
    @user_books = []
    @user_books = @user_books.concat(@user.books).flatten

    if current_user.admin?
      @books = Book.where("organizer_id != #{@user.id}") - @user_books
    elsif current_user.publisher?
      # data scope check
      if !User.joins(:books).where('books.id in (?)', current_user.organized_books).include?(@user)
        redirect_to signin_path
        return
      end

      @books = current_user.organized_books - @user_books
    end
  end

  def add_book
    @user = User.find(params[:id])
    @book = Book.find(params[:user][:book_id])

    # data scope check
    if current_user.publisher?
      if !User.joins(:books).where('books.id in (?)', current_user.organized_books).include?(@user)
        redirect_to signin_path
        return
      end

      if !current_user.organized_books.include?(@book)
        redirect_to signin_path
        return
      end
    end

    @user.books << @book

    if @user.save
      redirect_to admin_users_path, :notice => "Livro foi adicionado para o usuário."
    else
      render :books
    end
  end

  def remove_book
    @user = User.find(params[:id])
    @book = Book.find(params[:book_id])

    # data scope check
    if current_user.publisher?
      if !User.joins(:books).where('books.id in (?)', current_user.organized_books).include?(@user)
        redirect_to signin_path
        return
      end

      if !current_user.organized_books.include?(@book)
        redirect_to signin_path
        return
      end
    end

    @user.books.delete(@book)

    if @user.save
      redirect_to admin_users_path, :notice => "Foi removido o acesso do livro para o usuário."
    else
      render :books
    end
  end

end
{'fraction_non_alphanumeric': 0.12315653298835705, 'fraction_numerical': 0.0015523932729624838, 'mean_word_length': 2.854436689930209, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5281049', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1296, 'n_words': 284}