All pastes #975202 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-04-07 11:30 UTC
rendered paste body
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
);

CREATE TABLE permissions(
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  granted BOOLEAN NOT NULL DEFAULT false
);

CREATE TABLE roles (
  user_id INTEGER NOT NULL REFERENCES users,
  permission_it INTEGER NOT NULL REFERENCES permissions
);


controller/application.rb

class ApplicationController < ActionController::Base
  before_filter :check_permission

  protected

  def check_permission
    method = Retrieve.the.controller.name.plus.method
    begin
      return true if Role.find_by_sql ["SELECT 1 FROM roles r 
                                          INNER JOIN users u 
                                            ON u.id = r.user_id 
                                          INNER JOIN permissions p
                                            ON p.id = r.permission_id
                                          WHERE u.id = ? 
                                            AND p.name = ?
                                            AND p.granted IS TRUE", @me, method]
    rescue ActiveRecord::RecordNotFound
    end
  end
end