{"id":21451217,"url":"https://github.com/lenra-io/bouncer","last_synced_at":"2025-10-14T05:18:13.429Z","repository":{"id":43322735,"uuid":"382040068","full_name":"lenra-io/bouncer","owner":"lenra-io","description":"A simple elixir library to handle permissions","archived":false,"fork":false,"pushed_at":"2022-03-08T09:38:43.000Z","size":32,"stargazers_count":2,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-23T12:26:07.899Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lenra-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-01T13:20:30.000Z","updated_at":"2022-03-18T08:52:12.000Z","dependencies_parsed_at":"2022-09-05T18:22:20.428Z","dependency_job_id":null,"html_url":"https://github.com/lenra-io/bouncer","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lenra-io%2Fbouncer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lenra-io%2Fbouncer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lenra-io%2Fbouncer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lenra-io%2Fbouncer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lenra-io","download_url":"https://codeload.github.com/lenra-io/bouncer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243963838,"owners_count":20375679,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T04:19:37.313Z","updated_at":"2025-10-14T05:18:08.393Z","avatar_url":"https://github.com/lenra-io.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv id=\"top\"\u003e\u003c/div\u003e\n\u003c!--\n*** This README was created with https://github.com/othneildrew/Best-README-Template\n--\u003e\n\n\n\n\u003c!-- PROJECT SHIELDS --\u003e\n[![Contributors][contributors-shield]][contributors-url]\n[![Forks][forks-shield]][forks-url]\n[![Stargazers][stars-shield]][stars-url]\n[![Issues][issues-shield]][issues-url]\n[![MIT License][license-shield]][license-url]\n\n\n\n\u003c!-- PROJECT LOGO --\u003e\n\u003cbr /\u003e\n\u003cdiv align=\"center\"\u003e\n\n\u003ch3 align=\"center\"\u003eBouncer\u003c/h3\u003e\n\n  \u003cp align=\"center\"\u003e\n    Bouncer is a simple library that allow to create your permission policy using a simple behavior. It was built with \u003ca href=\"https://www.phoenixframework.org/\"\u003ePhoenix framework\u003c/a\u003e in mind but can be adapted to many more situations.\u003cbr /\u003e\n    Built With \u003ca href=\"https://elixir-lang.org/\"\u003eElixir\u003c/a\u003e\n    \u003c!-- \u003cbr /\u003e\n    \u003ca href=\"https://github.com/lenra-io/bouncer\"\u003e\u003cstrong\u003eExplore the docs »\u003c/strong\u003e\u003c/a\u003e --\u003e\n    \u003cbr /\u003e\n    \u003cbr /\u003e\n    \u003c!-- \u003ca href=\"https://github.com/lenra-io/bouncer\"\u003eView Demo\u003c/a\u003e ·--\u003e\n    \u003ca href=\"https://github.com/lenra-io/bouncer/issues\"\u003eReport Bug\u003c/a\u003e\n    ·\n    \u003ca href=\"https://github.com/lenra-io/bouncer/issues\"\u003eRequest Feature\u003c/a\u003e\n  \u003c/p\u003e\n\u003c/div\u003e\n\n\n\n\n\u003c!-- GETTING STARTED --\u003e\n\n## Installation\n\nThe package is curently not available in [hexpm](https://hex.pm/) but we are planning to add it in a near future.\n\nIn the meantime you can add it to your project dependancies using git.\n\n```elixir\ndef deps do\n  [\n    {:bouncer, git: \"https://github.com/lenra-io/bouncer.git\", tag: \"vx.y.z\"}\n  ]\nend\n```\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n\u003c!-- USAGE EXAMPLES --\u003e\n## Usage\n\nBouncer is designed around the ```Bouncer.Policy``` behavior. You just have to create a ```MyApp.Policy``` module and implement the ```authorize/3``` function.\nThe following example are used with Phoenix controllers.\n\n```elixir\n\n# First we define the Policy using the behavior for a specific Controller\ndefmodule MyApp.MyController.Policy do\n  @behaviour Bouncer.Policy\n\n  @impl true\n  # The authorize/3 function take:  \n  # - the atom representing the current user action\n  # - the user struct/map or anything that represent a resource/user/account\n  # - Any metadata useful to check permissions\n\n  # If the user is an admin, he can do everything\n  def authorize(_, %User{role: :admin}, _), do: true\n  # any one can acces the index page\n  def authorize(:index, _, _), do: true\n  # Only verified user can create the resource\n  def authorize(:create, %User{role: :verified_user}, _), do: true\n  # Only the owner of the resource can update or delete the resource.\n  # We use pattern matching on id to ensure that the id is the same.\n  # If the id of the user and the user owner is different, it will not match.\n  # We also use guard to group the rule of :create and :delete\n  def authorize(action, %User{id: id}, %Resource{owner: User{id: id}}) when action in [:create, :delete], do: true\n  # Good practice, deny everything else baseline.\n  def authorize(_, _, _), do: false\nend\n```\n\nNow that the policy is defined, you just have to use ```Bouncer.allow/4``` or ```Bouncer.allow?/4``` to check if the given resource can do the given action with a given metadata.\n\n```elixir\ndefmodule MyApp do\n  # The allow/3 function is designed to be used inside the `with` flow control.\n  # the allow?/3 function can be use in Enum.filter, if...\n  def index do\n    with {:ok, user} \u003c- fetch_my_user(),\n          :ok \u003c- Bouncer.allow(MyApp.Policy, :index, user, nil) do\n      do_my_stuff()\n    end\n  end\nend\n\n```\n\nSince this can add a lot of repetitive code such as fetching user, specify the module and the action, you can also create some macro to help you shorten the ```Bouncer.allow``` call.\n\nIn this example, we will create a macro that will automatically extract the user from the conn and pass the Policy module and the controller action. This will transform the allow/4 function into a allow/2 function that take the conn and the metadata.\n\n```elixir\ndefmodule MyApp.Policy do\n\n  defmacro __using__(opts \\\\ []) do\n    policy_module = Keyword.get(opts, :module)\n\n    quote do\n      @spec allow(any(), any()) :: :ok | {:error, atom()}\n      def allow(conn, params \\\\ nil) do\n        Bouncer.allow(\n          unquote(policy_module),\n          Plug.action_name(conn),\n          get_user(conn),\n          params\n        )\n      end\n    end\n  end\nend\n\n```\n\nThen, use it on the Phoenix controller like so\n```elixir\ndefmodule MyApp.MyController do\n  use MyApp, :controller\n\n  use MyApp.Policy,\n    module: MyApp.MyController.Policy\n\n  def index(conn, %{id: resource_id}) do\n    with {:ok, resource} \u003c- fetchResource(resource_id)\n        :ok \u003c- allow(conn, resource) do\n      doStuff()\n    end\n  end\n\n\nend\n```\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n\n\u003c!-- CONTRIBUTING --\u003e\n## Contributing\n\nContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\nIf you have a suggestion that would make this better, please open an issue with the tag \"enhancement\".\nDon't forget to give the project a star if you liked it! Thanks again!\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n\n\u003c!-- LICENSE --\u003e\n## License\n\nDistributed under the **MIT** License. See [LICENSE](./LICENSE) for more information.\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n\n\u003c!-- CONTACT --\u003e\n## Contact\n\nLenra - [@lenra_dev](https://twitter.com/lenra_dev) - contact@lenra.io\n\nProject Link: [https://github.com/lenra-io/bouncer](https://github.com/lenra-io/bouncer)\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n\u003c!-- MARKDOWN LINKS \u0026 IMAGES --\u003e\n\u003c!-- https://www.markdownguide.org/basic-syntax/#reference-style-links --\u003e\n[contributors-shield]: https://img.shields.io/github/contributors/lenra-io/bouncer.svg?style=for-the-badge\n[contributors-url]: https://github.com/lenra-io/bouncer/graphs/contributors\n[forks-shield]: https://img.shields.io/github/forks/lenra-io/bouncer.svg?style=for-the-badge\n[forks-url]: https://github.com/lenra-io/bouncer/network/members\n[stars-shield]: https://img.shields.io/github/stars/lenra-io/bouncer.svg?style=for-the-badge\n[stars-url]: https://github.com/lenra-io/bouncer/stargazers\n[issues-shield]: https://img.shields.io/github/issues/lenra-io/bouncer.svg?style=for-the-badge\n[issues-url]: https://github.com/lenra-io/bouncer/issues\n[license-shield]: https://img.shields.io/github/license/lenra-io/bouncer.svg?style=for-the-badge\n[license-url]: https://github.com/lenra-io/bouncer/blob/master/LICENSE.txt\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flenra-io%2Fbouncer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flenra-io%2Fbouncer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flenra-io%2Fbouncer/lists"}