Exemple #1
0
        /// <summary>
        /// Gets or creates a <see cref="Permission"/> instance for the named role
        /// on the object.
        /// </summary>
        /// <remarks>
        /// This function should be used in preference to manually querying for the
        /// applicable Permission as it ensures that there is exactly one Permission for
        /// the given Role on the object, merging duplicates or creating and adding new ones
        /// as needed.
        /// <para/>
        /// The given object must have a <c>IList&lt;Permission&gt;</c> property defined on it.
        /// If more than one such property exists, the first one will be used.
        /// </remarks>
        /// <param name="role">The <see cref="PermissionRole"/> associated with that Permission.</param>
        /// <param name="obj">
        /// The <see cref="RealmObject"/> inheritor whose permissions this
        /// <see cref="Permission"/> instance manipulates.
        /// </param>
        /// <returns>
        /// A <see cref="Permission"/> instance that can be used to inspect or modify the object
        /// permissions of that <see cref="PermissionRole"/>.
        /// </returns>
        public static Permission Get(PermissionRole role, RealmObject obj)
        {
            var permissionType = typeof(Permission).GetTypeInfo().GetMappedOrOriginalName();
            var prop           = obj.ObjectSchema.FirstOrDefault(o => o.Type == (PropertyType.Array | PropertyType.Object) && o.ObjectType == permissionType);

            if (prop.Name == null)
            {
                throw new ArgumentException("The given object doesn't have an IList<Permission> property.", nameof(obj));
            }

            var permissions = obj.GetListValue <Permission>(prop.Name);

            return(Get(role, permissions));
        }
Exemple #2
0
        /// <summary>
        /// Gets or creates a <see cref="Permission"/> instance for the named role
        /// on the collection.
        /// </summary>
        /// <remarks>
        /// This function should be used in preference to manually querying for the
        /// applicable Permission as it ensures that there is exactly one Permission for
        /// the given Role on the object, merging duplicates or creating and adding new ones
        /// as needed.
        /// </remarks>
        /// <param name="roleName">
        /// The name of the <see cref="PermissionRole"/> associated with that Permission. If no such
        /// Role exists, it will be created automatically.
        /// </param>
        /// <param name="permissions">
        /// The collection of permissions to which the new instance will be added.
        /// </param>
        /// <returns>
        /// A <see cref="Permission"/> instance that can be used to inspect or modify the object
        /// permissions of that <see cref="PermissionRole"/>.
        /// </returns>
        public static Permission Get(string roleName, IList <Permission> permissions)
        {
#if PCL
            RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
            return(null);
#else
            if (!(permissions is RealmList <Permission> realmPermissions))
            {
                throw new ArgumentException($"{nameof(Get)} may only be called on managed lists.", nameof(permissions));
            }

            var role = PermissionRole.Get(realmPermissions.Realm, roleName);
            return(Get(role, permissions));
#endif
        }
Exemple #3
0
        /// <summary>
        /// Gets or creates a <see cref="Permission"/> instance for the named role
        /// on the collection.
        /// </summary>
        /// <remarks>
        /// This function should be used in preference to manually querying for the
        /// applicable Permission as it ensures that there is exactly one Permission for
        /// the given Role on the object, merging duplicates or creating and adding new ones
        /// as needed.
        /// </remarks>
        /// <param name="role">The <see cref="PermissionRole"/> associated with that Permission.</param>
        /// <param name="permissions">
        /// The collection of permissions to which the new instance will be added.
        /// </param>
        /// <returns>
        /// A <see cref="Permission"/> instance that can be used to inspect or modify the object
        /// permissions of that <see cref="PermissionRole"/>.
        /// </returns>
        public static Permission Get(PermissionRole role, IList <Permission> permissions)
        {
#if PCL
            RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
            return(null);
#else
            if (!(permissions is RealmList <Permission> realmList))
            {
                throw new ArgumentException("Permission.Get may only be called on managed objects.");
            }

            if (!realmList.Realm.IsInTransaction)
            {
                throw new ArgumentException("Permissions may only be obtained or created in a write transaction.");
            }

            Permission result   = null;
            var        filtered = realmList.Where(p => p.Role.Equals(role)).ToArray();
            foreach (var permission in filtered)
            {
                if (result == null)
                {
                    result = permission;
                }
                else
                {
                    foreach (var propertyToMerge in _propertiesToMerge)
                    {
                        MergePermission(result, permission, propertyToMerge);
                    }

                    realmList.Remove(permission);
                }
            }

            if (result == null)
            {
                result = realmList.Realm.Add(new Permission
                {
                    Role = role
                });
                realmList.Add(result);
            }

            return(result);
#endif
        }
Exemple #4
0
 /// <summary>
 /// Gets or creates a <see cref="Permission"/> instance for the named role
 /// on the object.
 /// </summary>
 /// <remarks>
 /// This function should be used in preference to manually querying for the
 /// applicable Permission as it ensures that there is exactly one Permission for
 /// the given Role on the object, merging duplicates or creating and adding new ones
 /// as needed.
 /// <para/>
 /// The given object must have a <c>IList&lt;Permission&gt;</c> property defined on it.
 /// If more than one such property exists, the first one will be used.
 /// </remarks>
 /// <param name="roleName">
 /// The name of the <see cref="PermissionRole"/> associated with that Permission. If no such
 /// Role exists, it will be created automatically.
 /// </param>
 /// <param name="obj">
 /// The <see cref="RealmObject"/> inheritor whose permissions this
 /// <see cref="Permission"/> instance manipulates.
 /// </param>
 /// <returns>
 /// A <see cref="Permission"/> instance that can be used to inspect or modify the object
 /// permissions of that <see cref="PermissionRole"/>.
 /// </returns>
 public static Permission Get(string roleName, RealmObject obj)
 {
     return(Get(PermissionRole.Get(obj.Realm, roleName), obj));
 }
Exemple #5
0
 /// <summary>
 /// Gets or creates a <see cref="Permission"/> instance for the named role
 /// on the class.
 /// </summary>
 /// <remarks>
 /// This function should be used in preference to manually querying for the
 /// applicable Permission as it ensures that there is exactly one Permission for
 /// the given Role on the class, merging duplicates or creating and adding new ones
 /// as needed.
 /// </remarks>
 /// <param name="roleName">
 /// The name of the <see cref="PermissionRole"/> associated with that Permission. If no such
 /// Role exists, it will be created automatically.
 /// </param>
 /// <param name="className">
 /// The  name of the <see cref="RealmObject"/> subclass whose corresponding class permissions this
 /// <see cref="Permission"/> instance manipulates.
 /// </param>
 /// <param name="realm">The Realm whose class permissions this <see cref="Permission"/> instance manipulates.</param>
 /// <returns>
 /// A <see cref="Permission"/> instance that can be used to inspect or modify the class
 /// permissions of that <see cref="PermissionRole"/>.
 /// </returns>
 public static Permission Get(string roleName, string className, Realm realm)
 {
     return(Get(PermissionRole.Get(realm, roleName), ClassPermission.Get(realm, className).Permissions));
 }
Exemple #6
0
 /// <summary>
 /// Gets or creates a <see cref="Permission"/> instance for the named role
 /// on the class.
 /// </summary>
 /// <remarks>
 /// This function should be used in preference to manually querying for the
 /// applicable Permission as it ensures that there is exactly one Permission for
 /// the given Role on the class, merging duplicates or creating and adding new ones
 /// as needed.
 /// </remarks>
 /// <typeparam name="T">
 /// The <see cref="RealmObject"/> subclass whose corresponding class permissions this
 /// <see cref="Permission"/> instance manipulates.
 /// </typeparam>
 /// <param name="roleName">
 /// The name of the <see cref="PermissionRole"/> associated with that Permission. If no such
 /// Role exists, it will be created automatically.
 /// </param>
 /// <param name="realm">The Realm whose class permissions this <see cref="Permission"/> instance manipulates.</param>
 /// <returns>
 /// A <see cref="Permission"/> instance that can be used to inspect or modify the class
 /// permissions of that <see cref="PermissionRole"/>.
 /// </returns>
 public static Permission Get <T>(string roleName, Realm realm) where T : RealmObject
 {
     return(Get(PermissionRole.Get(realm, roleName), ClassPermission.Get <T>(realm).Permissions));
 }
Exemple #7
0
 /// <summary>
 /// Gets or creates a <see cref="Permission"/> instance for the named role
 /// on the Realm.
 /// </summary>
 /// <remarks>
 /// This function should be used in preference to manually querying for the
 /// applicable Permission as it ensures that there is exactly one Permission for
 /// the given Role on the Realm, merging duplicates or creating and adding new ones
 /// as needed.
 /// </remarks>
 /// <param name="roleName">
 /// The name of the <see cref="PermissionRole"/> associated with that Permission. If no such
 /// Role exists, it will be created automatically.
 /// </param>
 /// <param name="realm">The Realm whose permissions this <see cref="Permission"/> instance manipulates.</param>
 /// <returns>
 /// A <see cref="Permission"/> instance that can be used to inspect or modify the Realm
 /// permissions of that <see cref="PermissionRole"/>.
 /// </returns>
 public static Permission Get(string roleName, Realm realm)
 {
     return(Get(PermissionRole.Get(realm, roleName), realm));
 }
Exemple #8
0
 /// <summary>
 /// Gets or creates a <see cref="Permission"/> instance for the named role
 /// on the class.
 /// </summary>
 /// <remarks>
 /// This function should be used in preference to manually querying for the
 /// applicable Permission as it ensures that there is exactly one Permission for
 /// the given Role on the class, merging duplicates or creating and adding new ones
 /// as needed.
 /// </remarks>
 /// <typeparam name="T">
 /// The <see cref="RealmObject"/> subclass whose corresponding class permissions this
 /// <see cref="Permission"/> instance manipulates.
 /// </typeparam>
 /// <param name="role">The <see cref="PermissionRole"/> associated with that Permission.</param>
 /// <param name="realm">The Realm whose class permissions this <see cref="Permission"/> instance manipulates.</param>
 /// <returns>
 /// A <see cref="Permission"/> instance that can be used to inspect or modify the class
 /// permissions of that <see cref="PermissionRole"/>.
 /// </returns>
 public static Permission Get <T>(PermissionRole role, Realm realm) where T : RealmObject
 {
     return(Get(role, ClassPermission.Get <T>(realm).Permissions));
 }
Exemple #9
0
 /// <summary>
 /// Gets or creates a <see cref="Permission"/> instance for the named role
 /// on the Realm.
 /// </summary>
 /// <remarks>
 /// This function should be used in preference to manually querying for the
 /// applicable Permission as it ensures that there is exactly one Permission for
 /// the given Role on the Realm, merging duplicates or creating and adding new ones
 /// as needed.
 /// </remarks>
 /// <param name="role">The <see cref="PermissionRole"/> associated with that Permission.</param>
 /// <param name="realm">The Realm whose permissions this <see cref="Permission"/> instance manipulates.</param>
 /// <returns>
 /// A <see cref="Permission"/> instance that can be used to inspect or modify the Realm
 /// permissions of that <see cref="PermissionRole"/>.
 /// </returns>
 public static Permission Get(PermissionRole role, Realm realm)
 {
     return(Get(role, RealmPermission.Get(realm).Permissions));
 }
Exemple #10
0
 /// <summary>
 /// Gets or creates a <see cref="Permission"/> instance for the named role
 /// on the object.
 /// </summary>
 /// <remarks>
 /// This function should be used in preference to manually querying for the
 /// applicable Permission as it ensures that there is exactly one Permission for
 /// the given Role on the object, merging duplicates or creating and adding new ones
 /// as needed.
 /// <para/>
 /// The given object must have a <c>IList&lt;Permission&gt;</c> property defined on it.
 /// If more than one such property exists, the first one will be used.
 /// </remarks>
 /// <param name="roleName">
 /// The name of the <see cref="PermissionRole"/> associated with that Permission. If no such
 /// Role exists, it will be created automatically.
 /// </param>
 /// <param name="obj">
 /// The <see cref="RealmObject"/> inheritor whose permissions this
 /// <see cref="Permission"/> instance manipulates.
 /// </param>
 /// <returns>
 /// A <see cref="Permission"/> instance that can be used to inspect or modify the object
 /// permissions of that <see cref="PermissionRole"/>.
 /// </returns>
 public static Permission Get(string roleName, RealmObject obj)
 {
     Argument.NotNull(obj, nameof(obj));
     return(Get(PermissionRole.Get(obj.Realm, roleName), obj));
 }