concept admin in category ruby

appears as: dmins, n admin, admin, dmin, Admins, An admin, admins, The admins
Rails 4 in Action: Revised Edition of Rails 3 in Action

This is an excerpt from Manning's book Rails 4 in Action: Revised Edition of Rails 3 in Action.

The attr_accessible approach caused problems because different controllers might want to make available different sets of parameters for the same model, depending on the context. For instance, an admin area of the site may want to permit different fields than the user-facing area of the site—admins might have more fields, such as the ability to set the owner of a project.

Listing 7.40. Testing that you can create admins via our admin interface
scenario "when the new user is an admin" do fill_in "Email", with: "admin@example.com" fill_in "Password", with: "password" check "Is an admin?" click_button "Create User" expect(page).to have_content "User has been created." expect(page).to have_content "admin@example.com (Admin)" end

When you run bundle exec rspec spec/features/admin/creating_users_spec.rb, it fails when it attempts to select the “Is an admin?” check box:

1) Admins can create new users when the new user is an admin
   Failure/Error: check "Is an admin?"
   Capybara::ElementNotFound:
     Unable to find checkbox "Is an admin?"

7.1. Turning users into admins

To start the process of restricting the creation of projects to admins, you need to add an admin attribute to User objects. Only users who have this admin attribute set to true will be able to create projects. You can start enforcing this first via your existing tests—you have a feature spec for creating projects, and you can make sure the user creating the projects is an admin.

Listing 7.1. Logging in as an admin before creating a project
before do login_as(FactoryGirl.create(:user, :admin)) ...
Rails 3 in Action

This is an excerpt from Manning's book Rails 3 in Action.

The devise call is followed by a call to attr_accessible. This method defines fields that are accessible via attribute mass-assignment. Attribute mass-assignment happens when you pass a whole slew of attributes to a method such as create or update_attributes; because these methods take any and all parameters passed to them by default, users may attempt to hack the form and set an attribute they are not supposed to set, such as an admin boolean attribute. By using attr_accessible, you define a white list of fields you want the user to access. Any other fields passed through in an attribute mass-assignment are ignored.

git add .
git commit -
     m "Lock down specific projects controller actions for admins only"
git push

In this section, you ensured that only users with the admin attribute set to true can get to specific actions in your ProjectsController as an example of basic authorization.

Now when you run bin/cucumber features/creating_users.feature, it fails on the "Is an admin?" step:

7.1. Projects can be created only by admins

To restrict the creation of projects to admins, you alter the existing Background in features/creating_projects.feature and insert the following listing as the first three lines.

Figure 8.6. What admins see