magpie.api.management.group.group_utils¶
Attributes¶
Classes¶
Supported Webhook actions. |
|
Explicit definition of a |
|
Applicable types of Permission according to context. |
Functions¶
|
Obtains the JSON formatted Group definition according to field selection flags. |
|
Formats a Resource information into JSON. |
Checks if a permission is valid to be applied to a specific service or a resource under a root service. |
|
Generates the Webhook parameters based on provided references. |
|
|
Checks the config for any webhooks that correspond to the input action, and prepares corresponding requests. |
|
Obtains the formatted permission representations after validation that each of their name is a known member of |
|
Get all existing group names from the database. |
|
Get formatted JSON body describing all service resources the |
|
Creates a group if it is permitted and not conflicting. |
Obtains the group service/resource permission that corresponds to the provided one. |
|
Creates a permission on a group/resource combination if it is permitted and not conflicting. |
|
Get a dictionary of resources and corresponding permissions that a group has on the resources. |
|
Get validated response with group resource permissions as content. |
|
Get validated response on deleted group resource permission. |
|
|
Nest and regroup the resource permissions under corresponding root service types. |
Get validated response of services the group has permissions on. |
|
Get all permissions the group has on a specific service. |
|
Get validated response of found group service permissions. |
|
Get all permissions the group has on a specific service's children resources. |
|
Get validated response of all found service resources which the group has permissions on. |
Module Contents¶
- magpie.api.management.group.group_utils.format_group(group: magpie.models.Group, basic_info: bool = False, public_info: bool = False, dotted: bool = False, db_session: sqlalchemy.orm.session.Session | None = None) magpie.typedefs.JSON[source]¶
Obtains the JSON formatted Group definition according to field selection flags.
- Parameters:
group – Group for which to provide details.
basic_info – If
True, return only sufficient details to identify the group (useful for routes that refer to a group, but that are not requesting it specifically), or return full details (for routes that specifically request its information, e.g.:GET /groups/{grp}).public_info – Indicate if the returned details are intended for public information (
True) or admin-only (False). Only higher level users should be provided additional details to avoid leaking potentially sensitive parameters.dotted – Employ a dot (
.) instead of underscore (_) to separate Group from its basic information.db_session – Database connection to retrieve additional details (required when
public_info=False).
- magpie.api.management.group.group_utils.format_resource(resource: magpie.models.Resource, permissions: Collection[magpie.typedefs.AnyPermissionType] | None = None, permission_type: magpie.permissions.PermissionType | None = None, basic_info: bool = False, dotted: bool = False) magpie.typedefs.JSON[source]¶
Formats a Resource information into JSON.
- Parameters:
resource – Resource to be formatted.
permissions – Permissions to list along with the
resource. By default, these are the applicable permissions for that corresponding resource type.permission_type – Override indication of provenance to apply to
permissions. Only applicable when they are provided.basic_info – If
True, return only sufficient details to identify the resource, without any additionalpermissionsdetail, nor hierarchicalresourceinformation is returned.dotted – Employ a dot (
.) instead of underscore (_) to separate Resource from its basic information.
- magpie.api.management.group.group_utils.check_valid_service_or_resource_permission(permission_name: magpie.typedefs.Str | magpie.permissions.Permission, service_or_resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) magpie.permissions.Permission | None[source]¶
Checks if a permission is valid to be applied to a specific service or a resource under a root service.
- Parameters:
permission_name – permission name to be validated
service_or_resource – resource item corresponding to either a Service or a Resource
db_session – db connection
- Returns:
valid Permission if allowed by the service/resource
- Raises:
HTTPBadRequest – if the permission is not valid for the targeted service/resource
- class magpie.api.management.group.group_utils.WebhookAction[source]¶
Bases:
magpie.utils.ExtendedEnumSupported Webhook actions.
- UPDATE_USER_STATUS = 'update_user_status'¶
Triggered when an existing User status gets successfully updated.
See also
- CREATE_USER_PERMISSION = 'create_user_permission'¶
Triggered when a Permission onto a Service or Resource gets created for a User.
See also
- DELETE_USER_PERMISSION = 'delete_user_permission'¶
Triggered when a Permission onto a Service or Resource gets deleted for a User.
See also
- CREATE_GROUP_PERMISSION = 'create_group_permission'¶
Triggered when a Permission onto a Service or Resource gets created for a Group.
See also
- DELETE_GROUP_PERMISSION = 'delete_group_permission'¶
Triggered when a Permission onto a Service or Resource gets deleted for a Group.
See also
- magpie.api.management.group.group_utils.get_permission_update_params(target: magpie.models.User | magpie.models.Group, resource: magpie.typedefs.ServiceOrResourceType, permission: magpie.permissions.PermissionSet, db_session: sqlalchemy.orm.session.Session) magpie.typedefs.WebhookTemplateParameters[source]¶
Generates the Webhook parameters based on provided references.
- magpie.api.management.group.group_utils.process_webhook_requests(action: WebhookAction, params: magpie.typedefs.WebhookTemplateParameters, update_user_status_on_error: bool = False, settings: magpie.typedefs.AnySettingsContainer | None = None) None[source]¶
Checks the config for any webhooks that correspond to the input action, and prepares corresponding requests.
- Parameters:
action – tag identifying which webhooks to use in the config
params – Dictionary containing the required parameters and associated values for the request following the event action. Parameters will replace templates found in the
payloaddefinition of the webhook.update_user_status_on_error – update the user status or not in case of a webhook error.
settings – application settings where webhooks configuration can be retrieved.
- class magpie.api.management.group.group_utils.PermissionSet(permission: magpie.typedefs.AnyPermissionType, access: Access | magpie.typedefs.Str | None = None, scope: Scope | magpie.typedefs.Str | None = None, typ: PermissionType | None = None, reason: magpie.typedefs.Str | None = None)[source]¶
Bases:
objectExplicit definition of a
Permissionwith applicableAccessandScopeto resolve it.The
Permissionis the name of the applicable permission on themagpie.models.Resource. TheScopedefines how thePermissionshould impact the resolution of the perceived Effective Permissions over amagpie.models.Resourcetree hierarchy. TheAccessdefines how thePermissionaccess should be interpreted (granted or denied).Optionally, a
PermissionTypecan be provided to specifically indicate which kind of permission this set represents. This type is only for informative purposes, and is not saved to database nor displayed by the explicit string representation. It is returned within JSON representation and can be employed by Effective Permissions resolution to be more verbose about returned results.On missing
AccessorScopespecifications, they default toAccess.ALLOWandScope.RECURSIVEto handle backward compatible naming convention of plainpermission_name.Initializes the permission definition, possibly using required conversion from other implementations.
- Parameters:
permission – Name of the permission, or any other implementation from which the name can be inferred.
access – Effective behaviour of the permissions. Generally, grant or deny the specified permission.
scope – Scope for which the permission affects hierarchical resources. Important for effective resolution.
typ – Type of permission being represented. Informative only, does not impact behavior if omitted.
reason – Slightly more indicative information on why the current permission-type has this value. Value should be either explicitly provided or will be inferred if converted from input PermissionTuple.
See also
- __slots__ = ['_name', '_access', '_scope', '_tuple', '_type', '_reason']¶
- __lt__(other: Any) bool[source]¶
Ascending sort of permission according to their name, access and scope modifiers.
First sort by permission name alphabetically, followed by increasing restrictive access and increasing range of scoped resources.
Using this sorting methodology, similar permissions by name are grouped together first, and permissions of same name with modifiers are then ordered, the first having less priority when selecting a single item to display with conflicting possibilities. Respecting
Access.DENYis more important thanAccess.ALLOW(to protect the Resource), andScope.MATCHis closer to the actual Resource thanScope.RECURSIVEpermission received from a farther parent in the hierarchy.Sorted explicit string representation becomes:
[name1]-[allow]-[match] [name1]-[allow]-[recursive] [name1]-[deny]-[match] [name1]-[deny]-[recursive] [name2]-[allow]-[match] [name2]-[allow]-[recursive] [name2]-[deny]-[match] [name2]-[deny]-[recursive] ...
- We then obtain two crucial ordering results:
We can easily pick the last sorted item with highest resolution priority to find the final result of corresponding permissions. (note: final result for same user or group, their direct/inherited resolution is not considered here).
Picking the first element with lowest priority also displays the permission that impacts the widest range of resources. For instance in Magpie UI, indicating that a permission as
Scope.RECURSIVEis more verbose as it tell other resources under it are also receive the specifiedAccessmodifier rather than only the punctual resource.
Warning
Alphabetically sorting permissions by string representation (implicit/explicit) is not equivalent to sorting them according to Permission priority according to how modifiers are resolved. To obtain the prioritized sorting as strings, a list of
PermissionSet(with the strings as input) should be used to convert and correctly interpreted the raw strings, and then be converted back after sorting.# valid priority-sorted strings [str(perm) for perm in sorted(PermissionSet(p) for p in permission_strings)] # not equivalent to raw sorting list(sorted(permission_strings))
- __str__() magpie.typedefs.Str[source]¶
Obtains the compound literal representation of the
PermissionSet.Employed for database storage supporting
zigguratformat.
- __repr__() magpie.typedefs.Str[source]¶
Obtains the visual representation of the
PermissionSet.
- like(other: Any) bool[source]¶
Evaluates if one permission is similar to another permission definition regardless of modifiers.
This is different than
==operator which will evaluate exactly equal permission definitions.
- json() magpie.typedefs.PermissionDict[source]¶
Obtains the JSON representation of this
PermissionSet.
- webhook_params() magpie.typedefs.JSON[source]¶
Obtain JSON representation employed for Webhook reference.
- ace(user_or_group: magpie.models.User | magpie.models.Group | None) magpie.typedefs.AccessControlEntryType[source]¶
Converts the
PermissionSetinto an ACE thatpyramidcan understand.
- property reason: magpie.typedefs.Str | None¶
Indicative reason of the returned value defined by
type()or inferred by thePermissionTuple.See also
combine()- Returns:
Single string that describes the reason (source) of the permission, or multiple strings if updated by combination of multiple permissions.
- classmethod resolve(permission1: magpie.typedefs.ResolvablePermissionType, permission2: magpie.typedefs.ResolvablePermissionType, context: PermissionType = PermissionType.INHERITED, multiple_choice: magpie.typedefs.Str | None = None) magpie.typedefs.ResolvablePermissionType[source]¶
Resolves provided permissions into a single one considering various modifiers and groups for a resource.
Permissions MUST have the same Permission name. The associated Resource on which the two compared permissions are applied on should also be the same This method SHOULD NOT be used by itself to obtain for Effective Permission since it does not handle multi-level Resource resolution. Resolution is accomplished in this case only for a given level in the tree hierarchy.
The comparison considers both the
AccessandScopeof every Inherited Permission of the User, as well as its Group memberships sorted by their priority.See also
magpie.api.management.user.user_utils.combine_user_group_permissions()
- Parameters:
permission1 – Permission to compare.
permission2 – Permission to compare.
context – Control the resolution context (local/effective) of the permissions (safeguard against invalid definitions).
multiple_choice – Alternate explanation to default
PERMISSION_REASON_MULTIPLEapplied if multiple Permission refer to distinct Group of equal priority and equivalent access definitions, meaning they are interchangeable without impacting resolution to access the same target Resource.
- Returns:
Permission with highest priority to resolve access a resource without considering scope.
- property group_priority: magpie.typedefs.GroupPriority | None¶
Priority accessor in case of group inherited permission resolved by
PermissionTuple.
- property perm_tuple: ziggurat_foundations.permissions.PermissionTuple | None¶
Get the original
PermissionTupleif available (PermissionSetmust have been created by one).
- property implicit_permission: magpie.typedefs.Str | None¶
Obtain the implicit string representation of the
PermissionSetas plainPermissionname.This representation is backward compatible with prior versions of Magpie where explicit representation of permission names in the database did not exist.
If the contained modifiers of the
PermissionSet(notably theAccess.DENY) result in a string representation that is not possible according to non existing permissions for older Magpie instances, the returned value will beNone.See also
explicit_permission()for the new representation.
- property explicit_permission: magpie.typedefs.Str¶
Obtain the explicit string representation of the
PermissionSet.This format is always guaranteed to be completely defined contrary to
implicit_permission().See also
__str__()(default string value).implicit_permission()for the old representation.
- property name: Permission¶
- permission¶
- property type: PermissionType | None¶
- classmethod _convert(permission: magpie.typedefs.AnyPermissionType) PermissionSet | None[source]¶
Converts any permission representation to the
PermissionSetwith applicable enum members.Supports older
Permissionrepresentation such that implicit conversion of permission name withoutaccessandscopevalues are padded with defaults. Also, pre-defined partial or full definition from literal string representation are parsed to generate thePermissionSetinstance.- Parameters:
permission – implicit or explicit permission name string, or any other known permission implementation
- Raises:
ValueError – when the permission name cannot be identified or parsed
- class magpie.api.management.group.group_utils.PermissionType[source]¶
Bases:
magpie.utils.ExtendedEnumApplicable types of Permission according to context.
- ACCESS = 'access'¶
- ALLOWED = 'allowed'¶
- APPLIED = 'applied'¶
- DIRECT = 'direct'¶
- INHERITED = 'inherited'¶
- EFFECTIVE = 'effective'¶
- OWNED = 'owned'¶
- magpie.api.management.group.group_utils.format_permissions(permissions: Collection[magpie.typedefs.AnyPermissionType] | None, permission_type: PermissionType | None = None, force_unique: bool = True) Dict[magpie.typedefs.Str, List[magpie.typedefs.Str] | magpie.typedefs.PermissionDict | magpie.typedefs.Str][source]¶
Obtains the formatted permission representations after validation that each of their name is a known member of
Permissionenum, and optionally with modifiers as defined byPermissionSet.The returned lists are sorted alphabetically by permission name, and then in order of resolution priority (from highest to lowest) for each subset or corresponding name.
The permissions are cleaned from any duplicate entries, unless
force_uniqueis specified to allow it. If no or emptypermissionsis provided, empty lists are returned.Note
Field
permission_namesprovides both the older implicit permission names and the newer explicit name representation. For this reason, there will be semantically “duplicate” permissions in that list, but there will not be any literal string duplicates. Implicit names are immediately followed by their explicit name, unless implicit names do not apply for the given permission (e.g.: whenAccess.DENYdid not exist). Only detailed and explicit JSON representations are provided in thepermissionslist.When
permission_typeis equal toPermissionType.ALLOWED, the collection of every applicablePermissionSetis automatically generated by expanding all combinations ofAccessandScopewith every providedPermissionname inpermissions. This allows more concise definition of allowed permissions undermagpie.services.Servicesand their children Resource by only definingPermissionnames without manually listing all variations ofPermissionSet.For other
permission_typevalues, which represent Applied Permission only explicitly providedpermissionsare returned, to effectively return the collection of active permissions.- Parameters:
permissions – multiple permissions of any implementation and type, to be rendered both as names and JSON.
permission_type – indication of the represented permissions to be formatted, for informative indication.
force_unique – whether to remove duplicate entries by association of name, access and scope or not.
- Returns:
JSON with the permissions listed as implicit+explicit names, as permission set objects, and their type.
- magpie.api.management.group.group_utils.get_all_group_names(db_session: sqlalchemy.orm.session.Session) List[magpie.typedefs.Str][source]¶
Get all existing group names from the database.
- magpie.api.management.group.group_utils.get_group_resources(group: magpie.models.Group, db_session: sqlalchemy.orm.session.Session, service_types: List[magpie.typedefs.Str] | None = None) magpie.typedefs.JSON[source]¶
Get formatted JSON body describing all service resources the
groupas permissions on.
- magpie.api.management.group.group_utils.create_group(group_name: magpie.typedefs.Str, description: magpie.typedefs.Str, discoverable: bool, terms: magpie.typedefs.Str, db_session: sqlalchemy.orm.session.Session) pyramid.httpexceptions.HTTPException[source]¶
Creates a group if it is permitted and not conflicting.
- Returns:
valid HTTP response on successful operations.
- Raises:
HTTPException – error HTTP response of corresponding situation.
- magpie.api.management.group.group_utils.get_similar_group_resource_permission(group: magpie.models.Group, resource: magpie.typedefs.ServiceOrResourceType, permission: magpie.permissions.PermissionSet, db_session: sqlalchemy.orm.session.Session) magpie.permissions.PermissionSet | None[source]¶
Obtains the group service/resource permission that corresponds to the provided one.
Lookup considers only similar applied permission such that other permission modifiers don’t affect comparison.
- magpie.api.management.group.group_utils.create_group_resource_permission_response(group: magpie.models.Group, resource: magpie.typedefs.ServiceOrResourceType, permission: magpie.permissions.PermissionSet, db_session: sqlalchemy.orm.session.Session, overwrite: bool = False) pyramid.httpexceptions.HTTPException[source]¶
Creates a permission on a group/resource combination if it is permitted and not conflicting.
- Parameters:
group – group for which to create/update the permission.
resource – service or resource for which to create the permission.
permission – permission with modifiers to be applied.
db_session – database connection.
overwrite – If the corresponding (group, resource, permission[name]) exists, there is a conflict. Conflict is considered only by permission-name regardless of other modifiers. If overwrite is
False, the conflict will be raised and not be applied. If overwrite isTrue, the permission modifiers will be replaced by the new ones, or created if missing.
- Returns:
valid HTTP response on successful operations.
- Raises:
HTTPException – error HTTP response of corresponding situation.
- magpie.api.management.group.group_utils.get_group_resources_permissions_dict(group: magpie.models.Group, db_session: sqlalchemy.orm.session.Session, resource_ids: Iterable[int] | None = None, resource_types: Iterable[magpie.typedefs.Str] | None = None) magpie.typedefs.ResourcePermissionMap[source]¶
Get a dictionary of resources and corresponding permissions that a group has on the resources.
Filter search by
resource_idsand/orresource_typesif specified.
- magpie.api.management.group.group_utils.get_group_resource_permissions_response(group: magpie.models.Group, resource: magpie.models.Resource, db_session: sqlalchemy.orm.session.Session) pyramid.httpexceptions.HTTPException[source]¶
Get validated response with group resource permissions as content.
- Returns:
valid HTTP response on successful operations.
- Raises:
HTTPException – error HTTP response of corresponding situation.
- magpie.api.management.group.group_utils.delete_group_resource_permission_response(group: magpie.models.Group, resource: magpie.typedefs.ServiceOrResourceType, permission: magpie.permissions.PermissionSet, db_session: sqlalchemy.orm.session.Session, similar: bool = True) pyramid.httpexceptions.HTTPException[source]¶
Get validated response on deleted group resource permission.
- Parameters:
group – group for which to delete the permission.
resource – service or resource for which to delete the permission.
permission – permission with modifiers to be deleted.
db_session – database connection.
similar – Allow matching provided permission against any similar database permission. Otherwise, must match exactly.
- Returns:
valid HTTP response on successful operations.
- Raises:
HTTPException – error HTTP response of corresponding situation.
- magpie.api.management.group.group_utils.get_group_services(resources_permissions_dict: magpie.typedefs.JSON, db_session: sqlalchemy.orm.session.Session, service_types: List[magpie.typedefs.Str] | None = None) magpie.typedefs.JSON[source]¶
Nest and regroup the resource permissions under corresponding root service types.
- magpie.api.management.group.group_utils.get_group_services_response(group: magpie.models.Group, db_session: sqlalchemy.orm.session.Session, service_types: List[magpie.typedefs.Str] | None = None) pyramid.httpexceptions.HTTPException[source]¶
Get validated response of services the group has permissions on.
- Returns:
valid HTTP response on successful operations.
- Raises:
HTTPException – error HTTP response of corresponding situation.
- magpie.api.management.group.group_utils.get_group_service_permissions(group: magpie.models.Group, service: magpie.models.Service, db_session: sqlalchemy.orm.session.Session) List[magpie.permissions.PermissionSet][source]¶
Get all permissions the group has on a specific service.
- magpie.api.management.group.group_utils.get_group_service_permissions_response(group: magpie.models.Group, service: magpie.models.Service, db_session: sqlalchemy.orm.session.Session) pyramid.httpexceptions.HTTPException[source]¶
Get validated response of found group service permissions.
- Returns:
valid HTTP response on successful operations.
- Raises:
HTTPException – error HTTP response of corresponding situation.
- magpie.api.management.group.group_utils.get_group_service_resources_permissions_dict(group: magpie.models.Group, service: magpie.models.Service, db_session: sqlalchemy.orm.session.Session) magpie.typedefs.ResourcePermissionMap[source]¶
Get all permissions the group has on a specific service’s children resources.
- magpie.api.management.group.group_utils.get_group_service_resources_response(group: magpie.models.Group, service: magpie.models.Service, db_session: sqlalchemy.orm.session.Session) pyramid.httpexceptions.HTTPException[source]¶
Get validated response of all found service resources which the group has permissions on.
- Returns:
valid HTTP response on successful operations.
- Raises:
HTTPException – error HTTP response of corresponding situation.