Phoenix for SAAS application

Ooo, I like the style, kind of a more refined version of what I’m doing now. I currently use my PermissionEx library to do the actual permission testing, and the canada library to test specific things, mine works like this:

First I define a behaviour I want, lets start with:

defmodule MyServer.PermsBehaviour do
  @callback get_as_new() :: %{}
end

Where get_as_new/0 just returns what is a good default structure for assigning this permission to someone.

I then define some permissions, say this for a simple example:

defmodule MyServer.Perms.Auth.Profile do
  @behaviour MyServer.PermsBehaviour
  defstruct action: nil, uid: :_

  def get_as_new(), do: %MyServer.Perms.Auth.Profile{action: "", uid: :_}
end

I can get all of my permission types in the entire program (say for populating a drop-down list in the admin section to assign someone permission and values and such) like this by using my PluginsEx library (I should probably release it someday, it is so useful, but such a huge hack…):

all_perms = PluginsEx.get_plugins(MyServer.PermsBehaviour)

Where all_perms is now a list of atoms, given the above permission it would just be [MyServer.Perms.Auth.Profile].

To use the permission via canada, an example:

alias MyServer.Perms

@perm true = conn |> can?(index(%Perms.Auth.Profile{uid: uid}))

And I can refine it over time, so for example I can do this:

@perm true = conn |> can?(index(%Perms.Auth.Profile{uid: :_}))
uid = get_uid_from_params(params)
@perm true = conn |> can?(index(%Perms.Auth.Profile{uid: uid}))

Or something like that, say if getting the uid is a costly db lookup and you don’t want to do it if they do not have access to any uid at all first. My PermissionEx library looks like it would work well with authorize, basically it would replace canary (which only returns true/false) with a better return structure and information (like a message). Looks useful. :slight_smile:

Great you like it! In the end everything can be written as a single evaluation, but for complex rules it can get gnarly and very hard to understand. This is why cancan was not a good enough solution for my use case.

Generally authorization tends to work like a decision tree with :ok or :error leaf nodes. Decision trees can be easily written down as rules that are evaluated sequentially. So that is the rational. Any feedback to the API is welcome. It seems to work great for our cloud app for now.

Yeah, that is precisely what my permission_ex library was designed to work around. Easy storage of fairly arbitrary permissions into a database and easy editing on the front-end, it works pretty well for me. :slight_smile:

Open sourced? Can’t seem to find it on github.

https://github.com/OvermindDL1/permission_ex

1 Like

Yes that. ^.^

CamelCase for the module name, snake_case for the project name, common convention I’ve seen in the Elixir ecosystem so I follow it. :slight_smile:

2 Likes

It seems that apartmentex currently does not support Ecto 2.1.* , do you know of other alternative?

1 Like

tenantex (fork of apartmentex) has an ecto 2.1 branch - https://github.com/promptworks/tenantex/tree/ecto2.1 - haven’t used it myself though.

2 Likes

I have used freiden fork, at https://github.com/freiden/apartmentex.git, it worked fine :slight_smile:

2 Likes