scope.ex (1019B)
1 defmodule DailyTracker.Accounts.Scope do 2 @moduledoc """ 3 Defines the scope of the caller to be used throughout the app. 4 5 The `DailyTracker.Accounts.Scope` allows public interfaces to receive 6 information about the caller, such as if the call is initiated from an 7 end-user, and if so, which user. Additionally, such a scope can carry fields 8 such as "super user" or other privileges for use as authorization, or to 9 ensure specific code paths can only be access for a given scope. 10 11 It is useful for logging as well as for scoping pubsub subscriptions and 12 broadcasts when a caller subscribes to an interface or performs a particular 13 action. 14 15 Feel free to extend the fields on this struct to fit the needs of 16 growing application requirements. 17 """ 18 19 alias DailyTracker.Accounts.User 20 21 defstruct user: nil 22 23 @doc """ 24 Creates a scope for the given user. 25 26 Returns nil if no user is given. 27 """ 28 def for_user(%User{} = user) do 29 %__MODULE__{user: user} 30 end 31 32 def for_user(nil), do: nil 33 end