2 * Copyright (C) 2013 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 * SECTION:rtsp-permissions
21 * @short_description: Roles and associated permissions
22 * @see_also: #GstRTSPToken, #GstRTSPAuth
24 * The #GstRTSPPermission object contains an array of roles and associated
25 * permissions. The roles are represented with a string and the permissions with
26 * a generic #GstStructure.
28 * The permissions are deliberately kept generic. The possible values of the
29 * roles and #GstStructure keys and values are only determined by the #GstRTSPAuth
30 * object that performs the checks on the permissions and the current
33 * As a convenience function, gst_rtsp_permissions_is_allowed() can be used to
34 * check if the permissions contains a role that contains the boolean value
35 * %TRUE for the the given key.
37 * Last reviewed on 2013-07-15 (1.0.0)
42 #include "rtsp-permissions.h"
44 typedef struct _GstRTSPPermissionsImpl
46 GstRTSPPermissions permissions;
48 /* Roles, array of GstStructure */
50 } GstRTSPPermissionsImpl;
53 free_structure (GstStructure * structure)
55 gst_structure_set_parent_refcount (structure, NULL);
56 gst_structure_free (structure);
59 //GST_DEBUG_CATEGORY_STATIC (rtsp_permissions_debug);
60 //#define GST_CAT_DEFAULT rtsp_permissions_debug
62 GST_DEFINE_MINI_OBJECT_TYPE (GstRTSPPermissions, gst_rtsp_permissions);
64 static void gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
65 GstStructure * structure);
68 _gst_rtsp_permissions_free (GstRTSPPermissions * permissions)
70 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
72 g_ptr_array_free (impl->roles, TRUE);
74 g_slice_free1 (sizeof (GstRTSPPermissionsImpl), permissions);
77 static GstRTSPPermissions *
78 _gst_rtsp_permissions_copy (GstRTSPPermissionsImpl * permissions)
80 GstRTSPPermissionsImpl *copy;
81 GstStructure *structure;
83 copy = g_slice_new0 (GstRTSPPermissionsImpl);
84 gst_rtsp_permissions_init (copy, structure);
86 return GST_RTSP_PERMISSIONS (copy);
90 gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
91 GstStructure * structure)
93 gst_mini_object_init (GST_MINI_OBJECT_CAST (permissions), 0,
94 GST_TYPE_RTSP_PERMISSIONS,
95 (GstMiniObjectCopyFunction) _gst_rtsp_permissions_copy, NULL,
96 (GstMiniObjectFreeFunction) _gst_rtsp_permissions_free);
99 g_ptr_array_new_with_free_func ((GDestroyNotify) free_structure);
103 * gst_rtsp_permissions_new:
105 * Create a new empty Authorization permissions.
107 * Returns: (transfer full): a new empty authorization permissions.
110 gst_rtsp_permissions_new (void)
112 GstRTSPPermissionsImpl *permissions;
114 permissions = g_slice_new0 (GstRTSPPermissionsImpl);
116 gst_rtsp_permissions_init (permissions,
117 gst_structure_new_empty ("GstRTSPPermissions"));
119 return GST_RTSP_PERMISSIONS (permissions);
123 * gst_rtsp_permissions_add_role:
124 * @permissions: a #GstRTSPPermissions
126 * @fielname: the first field name
127 * @...: additional arguments
129 * Add a new @role to @permissions with the given variables. The fields
130 * are the same layout as gst_structure_new().
133 gst_rtsp_permissions_add_role (GstRTSPPermissions * permissions,
134 const gchar * role, const gchar * fieldname, ...)
138 va_start (var_args, fieldname);
139 gst_rtsp_permissions_add_role_valist (permissions, role, fieldname, var_args);
144 * gst_rtsp_permissions_add_role_valist:
145 * @permissions: a #GstRTSPPermissions
147 * @fielname: the first field name
148 * @var_args: additional fields to add
150 * Add a new @role to @permissions with the given variables. Structure fields
151 * are set according to the varargs in a manner similar to gst_structure_new().
154 gst_rtsp_permissions_add_role_valist (GstRTSPPermissions * permissions,
155 const gchar * role, const gchar * fieldname, va_list var_args)
157 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
158 GstStructure *structure;
162 g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
163 g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
164 g_return_if_fail (role != NULL);
165 g_return_if_fail (fieldname != NULL);
167 structure = gst_structure_new_valist (role, fieldname, var_args);
168 g_return_if_fail (structure != NULL);
170 len = impl->roles->len;
172 for (i = 0; i < len; i++) {
173 GstStructure *entry = g_ptr_array_index (impl->roles, i);
175 if (gst_structure_has_name (entry, role)) {
176 gst_structure_free (entry);
182 gst_structure_set_parent_refcount (structure,
183 &impl->permissions.mini_object.refcount);
185 g_ptr_array_add (impl->roles, structure);
187 g_ptr_array_index (impl->roles, i) = structure;
191 * gst_rtsp_permissions_remove_role:
192 * @permissions: a #GstRTSPPermissions
195 * Remove all permissions for @role in @permissions.
198 gst_rtsp_permissions_remove_role (GstRTSPPermissions * permissions,
201 g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
202 g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
203 g_return_if_fail (role != NULL);
207 * gst_rtsp_permissions_get_role:
208 * @permissions: a #GstRTSPPermissions
211 * Get all permissions for @role in @permissions.
213 * Returns: the structure with permissions for @role.
216 gst_rtsp_permissions_get_role (GstRTSPPermissions * permissions,
219 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
222 g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), NULL);
223 g_return_val_if_fail (role != NULL, NULL);
225 len = impl->roles->len;
226 for (i = 0; i < len; i++) {
227 GstStructure *entry = g_ptr_array_index (impl->roles, i);
229 if (gst_structure_has_name (entry, role))
236 * gst_rtsp_permissions_is_allowed:
237 * @permissions: a #GstRTSPPermissions
239 * @permission: a permission
241 * Check if @role in @permissions is given permission for @permission.
243 * Returns: %TRUE if @role is allowed @permission.
246 gst_rtsp_permissions_is_allowed (GstRTSPPermissions * permissions,
247 const gchar * role, const gchar * permission)
249 const GstStructure *str;
252 g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), FALSE);
253 g_return_val_if_fail (role != NULL, FALSE);
254 g_return_val_if_fail (permission != NULL, FALSE);
256 str = gst_rtsp_permissions_get_role (permissions, role);
260 if (!gst_structure_get_boolean (str, permission, &result))