コード例 #1
0
        private Boolean?HasPermission <T>(LocalUserPrincipalModel principal, Enum permission)
        {
            // Get the interface to which permissions are applied
            Type runtimeType = typeof(T).IsInterface ? typeof(T) : typeof(T).GetInterfaces().Except(typeof(T).GetInterfaces().SelectMany(iface => iface.GetInterfaces())).FirstOrDefault();

            if (runtimeType == null && typeof(T).BaseType.GetGenericArguments().Length != 0 && typeof(T).BaseType.GetGenericArguments()[0].GetInterfaces().Any(iface => iface == typeof(IModel)))
            {
                runtimeType = typeof(T).BaseType.GetGenericArguments()[0].IsInterface ? typeof(T).BaseType.GetGenericArguments()[0] : typeof(T).BaseType.GetGenericArguments()[0].GetInterfaces().Except(typeof(T).BaseType.GetGenericArguments()[0].GetInterfaces().SelectMany(iface => iface.GetInterfaces())).FirstOrDefault();
            }

            // Get the permission set for the specified type (this will be null for types linked to an unlicensed module)
            String            typeName      = String.Format("{0}.{1}", runtimeType.Namespace, runtimeType.Name.Substring(1, runtimeType.Name.Length - 6));
            UserPermissionSet permissionSet = principal.PermissionSets.FirstOrDefault(ps => String.Equals(ps.FullName, typeName, StringComparison.OrdinalIgnoreCase));

            if (permissionSet == null)
            {
                return(false);
            }

            // Get the permission information that matches the specified permission (this will be null for types that do not support the specified permission)
            UserPermissionType permissionType = permissionSet.Permissions.FirstOrDefault(pt => String.Equals(pt.Type, permission.GetType().Name, StringComparison.OrdinalIgnoreCase) && pt.Number == permission.ConvertTo <Int32>());

            if (permissionType == null)
            {
                return(false);
            }

            // Return the permission setting
            return(permissionType.Value);
        }
コード例 #2
0
        private IEnumerable <UserPermissionSet> GetPermissions()
        {
            // Identify all of the modules that are currently available under the software license.
            LicenseModules[] licensedModules = this.Provider.Security.License.GetCurrent().Modules.Where(mod => mod.Enabled).Select(mod => mod.Module).ToArray();

            // Identify all of the runtime types that are derived from the <see cref="LocalModel" /> type.
            Type[] types = typeof(LocalModel).Assembly.GetTypes().Where(type => !type.IsAbstract && type.IsSubclassOf(typeof(LocalModel))).ToArray();
            foreach (Type type in types)
            {
                // Identify all of the modules that the type is restricted to.
                LicenseModules[] modules = type.GetCustomAttributes(false).OfType <LicenseModuleRestrictionAttribute>().SelectMany(attr => attr.Modules).ToArray();
                modules = (modules.Length == 0) ? new LicenseModules[] { LicenseModules.Core } : modules;

                // Validate that at least one of the modules is enabled under the software license.
                modules = modules.Intersect(licensedModules).ToArray();
                if (modules.Length != 0)
                {
                    UserPermissionSet permissionSet = new UserPermissionSet(modules.First(), type);

                    // Iterate through the permissions that are applicable to the runtime type.
                    UserPermissionAttribute[] attributes = type.GetCustomAttributes(false).OfType <UserPermissionAttribute>().ToArray();
                    Enum[] permissions = attributes.Length == 0 ? Enum.GetValues(typeof(UserPermissionActions)).OfType <Enum>().ToArray() : attributes.SelectMany(attr => attr.Permissions).ToArray();
                    foreach (Enum permission in permissions)
                    {
                        UserPermissionType permissionType = new UserPermissionType();
                        permissionType.Type   = permission.GetType().Name;
                        permissionType.Name   = permission.ConvertTo <String>();
                        permissionType.Number = permission.ConvertTo <Int32>();

                        // Apply the permissions saved against the current security principal.
                        LocalUserPermissionModel dbPermission = this.Permissions.FirstOrDefault(perm => String.Equals(permissionSet.FullName, perm.DataType, StringComparison.OrdinalIgnoreCase) && Int32.Equals(permissionType.Number, perm.Action));
                        if (dbPermission != null)
                        {
                            permissionType.Id    = dbPermission.Id;
                            permissionType.Value = dbPermission.Allow;
                        }
                        permissionSet.Permissions.Add(permissionType);
                    }

                    yield return(permissionSet);
                }
            }
        }
コード例 #3
0
        public void SetPermissions(ArrayList permissionSets)
        {
            // Update the in-memory collection of permissions
            foreach (Dictionary <String, Object> permissionSet in permissionSets)
            {
                UserPermissionSet principalPermissionSet = this.PermissionSets.FirstOrDefault(ps => ps.FullName == permissionSet["FullName"].ConvertTo <String>());
                if (principalPermissionSet != null)
                {
                    ArrayList permissionTypes = permissionSet["Permissions"] as ArrayList;
                    foreach (Dictionary <String, Object> permissionType in permissionTypes)
                    {
                        UserPermissionType principalPermissionType = principalPermissionSet.Permissions.FirstOrDefault(pt => pt.Number == permissionType["Number"].ConvertTo <Int32>());
                        if (principalPermissionType != null)
                        {
                            principalPermissionType.Value = permissionType["Value"].ConvertTo <Boolean?>();
                        }
                    }
                }
            }

            // Apply the changes to the underlying database
            foreach (UserPermissionSet principalPermissionSet in this.PermissionSets)
            {
                foreach (UserPermissionType principalPermissionType in principalPermissionSet.Permissions)
                {
                    if (principalPermissionType.Id == 0 && principalPermissionType.Value != null)
                    {
                        LocalUserPermissionModel model = new LocalUserPermissionModel(this.Provider)
                        {
                            PrincipalId = this.Id, DataType = principalPermissionSet.FullName, Action = principalPermissionType.Number, Allow = principalPermissionType.Value.Value
                        };
                        model.Save();
                    }
                    if (principalPermissionType.Id != 0 && principalPermissionType.Value == null)
                    {
                        LocalUserPermissionModel model = this.Provider.Security.UserPermission.FetchById(principalPermissionType.Id);
                        model.Delete();
                    }
                }
            }

            _permissionSets = null;
        }