https://github.com/ka8725/grut
Flexible authorization solution for Ruby projects.
https://github.com/ka8725/grut
authorization persistent ruby ruby-on-rails
Last synced: about 1 month ago
JSON representation
Flexible authorization solution for Ruby projects.
- Host: GitHub
- URL: https://github.com/ka8725/grut
- Owner: ka8725
- License: mit
- Created: 2017-04-28T21:53:54.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-25T22:04:14.000Z (over 7 years ago)
- Last Synced: 2025-02-17T08:36:29.831Z (4 months ago)
- Topics: authorization, persistent, ruby, ruby-on-rails
- Language: Ruby
- Size: 16.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Grut
Define user permissions in a Ruby project dynamically and store them in a database with Grut's help.
This allows to manage access to specific entities for concrete users on the fly through a user interface.## Installation
Grut requires already installed any of the database adapters supported by [sequel](https://github.com/jeremyevans/sequel). `pg` and `mysql2` are the most popular ones.
Add this line to your application's Gemfile:
```ruby
gem 'grut'
```Configure the database connection after that in some place of the project that have Grut installed. For example, it could be the following line in the `config/application.rb` of a Rails project:
```ruby
Grut::Config.instance.db_url = 'postgres://username:password@localhost/my_project_dev'
```> It's assumed that in that case there will be used `pg` gem and there will be created `my_project_dev` database
> and the `username` user with the `password` password has access to that database.And run migrations:
```
rake grut:install
```## Usage
There are two main classes: `Grut::Guardian` and `Grut::Statement`. Use `Grut::Guardian` to manage control
access for entries and `Grut::Statement` to get information about defined permissions for a given user.
Look into the following code snippet that demonstrates their usage:```ruby
user = Struct.new(:id).new(42)
store = Struct.new(:id).new(12)guardian = Grut::Guardian.new(user, :admin)
statement = Grut::Statement.new(user)guardian.permitted?(:manage_store, all: true) # => false
guardian.permitted?(:manage_store, id: store.id) # => false
statement.all #=> []guardian.permit(:manage_store, all: true)
guardian.permitted?(:manage_store, all: true) # => true
guardian.permitted?(:manage_store, id: store.id) # => true
statement.all #=> [#]guardian.forbid(:manage_store, all: true)
guardian.permitted?(:manage_store, all: true) # => false
guardian.permitted?(:manage_store, id: store.id) # => false
statement.all #=> []guardian.permit(:manage_store, id: 1)
guardian.permitted?(:manage_store, all: true) # => true
guardian.permitted?(:manage_store, id: store.id) # => true
statement.all #=> [#]
```## Development
Install PostgreSQL and add the following user with password:
```ruby
createuser -l -s grut
psql -d postgres -c "alter user grut with password 'password';"
```Run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ka8725/grut. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).