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 #GstRTSPPermissions 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);
67 _gst_rtsp_permissions_free (GstRTSPPermissions * permissions)
69 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
71 g_ptr_array_free (impl->roles, TRUE);
73 g_slice_free1 (sizeof (GstRTSPPermissionsImpl), permissions);
76 static GstRTSPPermissions *
77 _gst_rtsp_permissions_copy (GstRTSPPermissionsImpl * permissions)
79 GstRTSPPermissionsImpl *copy;
82 copy = (GstRTSPPermissionsImpl *) gst_rtsp_permissions_new ();
84 for (i = 0; i < permissions->roles->len; i++) {
85 GstStructure *entry = g_ptr_array_index (permissions->roles, i);
86 GstStructure *entry_copy = gst_structure_copy (entry);
88 gst_structure_set_parent_refcount (entry_copy,
89 ©->permissions.mini_object.refcount);
90 g_ptr_array_add (copy->roles, entry_copy);
93 return GST_RTSP_PERMISSIONS (copy);
97 gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions)
99 gst_mini_object_init (GST_MINI_OBJECT_CAST (permissions), 0,
100 GST_TYPE_RTSP_PERMISSIONS,
101 (GstMiniObjectCopyFunction) _gst_rtsp_permissions_copy, NULL,
102 (GstMiniObjectFreeFunction) _gst_rtsp_permissions_free);
105 g_ptr_array_new_with_free_func ((GDestroyNotify) free_structure);
109 * gst_rtsp_permissions_new:
111 * Create a new empty Authorization permissions.
113 * Returns: (transfer full): a new empty authorization permissions.
116 gst_rtsp_permissions_new (void)
118 GstRTSPPermissionsImpl *permissions;
120 permissions = g_slice_new0 (GstRTSPPermissionsImpl);
121 gst_rtsp_permissions_init (permissions);
123 return GST_RTSP_PERMISSIONS (permissions);
127 * gst_rtsp_permissions_add_role:
128 * @permissions: a #GstRTSPPermissions
130 * @fieldname: the first field name
131 * @...: additional arguments
133 * Add a new @role to @permissions with the given variables. The fields
134 * are the same layout as gst_structure_new().
137 gst_rtsp_permissions_add_role (GstRTSPPermissions * permissions,
138 const gchar * role, const gchar * fieldname, ...)
142 va_start (var_args, fieldname);
143 gst_rtsp_permissions_add_role_valist (permissions, role, fieldname, var_args);
148 * gst_rtsp_permissions_add_role_valist:
149 * @permissions: a #GstRTSPPermissions
151 * @fieldname: the first field name
152 * @var_args: additional fields to add
154 * Add a new @role to @permissions with the given variables. Structure fields
155 * are set according to the varargs in a manner similar to gst_structure_new().
158 gst_rtsp_permissions_add_role_valist (GstRTSPPermissions * permissions,
159 const gchar * role, const gchar * fieldname, va_list var_args)
161 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
162 GstStructure *structure;
165 g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
166 g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
167 g_return_if_fail (role != NULL);
168 g_return_if_fail (fieldname != NULL);
170 structure = gst_structure_new_valist (role, fieldname, var_args);
171 g_return_if_fail (structure != NULL);
173 len = impl->roles->len;
174 for (i = 0; i < len; i++) {
175 GstStructure *entry = g_ptr_array_index (impl->roles, i);
177 if (gst_structure_has_name (entry, role)) {
178 g_ptr_array_remove_index_fast (impl->roles, i);
183 gst_structure_set_parent_refcount (structure,
184 &impl->permissions.mini_object.refcount);
185 g_ptr_array_add (impl->roles, structure);
189 * gst_rtsp_permissions_remove_role:
190 * @permissions: a #GstRTSPPermissions
193 * Remove all permissions for @role in @permissions.
196 gst_rtsp_permissions_remove_role (GstRTSPPermissions * permissions,
199 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
202 g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
203 g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
204 g_return_if_fail (role != NULL);
206 len = impl->roles->len;
207 for (i = 0; i < len; i++) {
208 GstStructure *entry = g_ptr_array_index (impl->roles, i);
210 if (gst_structure_has_name (entry, role)) {
211 g_ptr_array_remove_index_fast (impl->roles, i);
218 * gst_rtsp_permissions_get_role:
219 * @permissions: a #GstRTSPPermissions
222 * Get all permissions for @role in @permissions.
224 * Returns: (transfer none): the structure with permissions for @role. It
225 * remains valid for as long as @permissions is valid.
228 gst_rtsp_permissions_get_role (GstRTSPPermissions * permissions,
231 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
234 g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), NULL);
235 g_return_val_if_fail (role != NULL, NULL);
237 len = impl->roles->len;
238 for (i = 0; i < len; i++) {
239 GstStructure *entry = g_ptr_array_index (impl->roles, i);
241 if (gst_structure_has_name (entry, role))
248 * gst_rtsp_permissions_is_allowed:
249 * @permissions: a #GstRTSPPermissions
251 * @permission: a permission
253 * Check if @role in @permissions is given permission for @permission.
255 * Returns: %TRUE if @role is allowed @permission.
258 gst_rtsp_permissions_is_allowed (GstRTSPPermissions * permissions,
259 const gchar * role, const gchar * permission)
261 const GstStructure *str;
264 g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), FALSE);
265 g_return_val_if_fail (role != NULL, FALSE);
266 g_return_val_if_fail (permission != NULL, FALSE);
268 str = gst_rtsp_permissions_get_role (permissions, role);
272 if (!gst_structure_get_boolean (str, permission, &result))