Exemplo n.º 1
0
        // Gets security information and exports it into the files.
        // excludeMetadataPermission indicates if model privileges for Metadata are exluded.
        private static void ExportSecurityInformation(bool excludeMetadataPermission, string fileName)
        {
            // Gets security information.
            SecurityPrincipalsGetRequest principalGetRequest = new SecurityPrincipalsGetRequest();
            principalGetRequest.Criteria = new SecurityPrincipalsCriteria();
            principalGetRequest.Criteria.All = true;
            principalGetRequest.Criteria.Type = PrincipalType.UserAccount;
            principalGetRequest.Criteria.ResultType = ResultType.Details;
            principalGetRequest.Criteria.SecurityResolutionType = SecurityResolutionType.Users;
            principalGetRequest.Criteria.ModelPrivilege = ResultType.Details;
            principalGetRequest.Criteria.FunctionPrivilege = ResultType.Details;
            principalGetRequest.Criteria.HierarchyMemberPrivilege = ResultType.Details;

            // Gets the security principals for all the users.
            SecurityPrincipalsGetResponse principalGetResponse = clientProxy.SecurityPrincipalsGet(principalGetRequest);
            HandleOperationErrors(principalGetResponse.OperationResult);

            System.Collections.ObjectModel.Collection<User> users = principalGetResponse.Principals.Users;

            // Exclude model privileges for Metadata when excludeMetadataPermission is true.
            if (excludeMetadataPermission)
            {
                foreach (User anUser in users)
                {
                    System.Collections.ObjectModel.Collection<ModelPrivilege> tempModelPrivileges = new System.Collections.ObjectModel.Collection<ModelPrivilege>{};

                    // Exclude model privileges for Metadata (internal id = 1).
                    foreach (ModelPrivilege aPrivilege in anUser.SecurityPrivilege.ModelPrivileges)
                    {
                        if (aPrivilege.ModelId.InternalId != 1)
                        {
                            tempModelPrivileges.Add(aPrivilege);
                        }
                    }

                    anUser.SecurityPrivilege.ModelPrivileges = tempModelPrivileges;
                }
            }

            principalGetRequest.Criteria.Type = PrincipalType.Group;
            principalGetRequest.Criteria.SecurityResolutionType = SecurityResolutionType.UserAndGroup;

            // Gets the security principals for all the groups.
            SecurityPrincipalsGetResponse principalGetGroupResponse = clientProxy.SecurityPrincipalsGet(principalGetRequest);
            HandleOperationErrors(principalGetGroupResponse.OperationResult);

            System.Collections.ObjectModel.Collection<Group> groups = principalGetGroupResponse.Principals.Groups;

            // Exclude model privileges for Metadata when excludeMetadataPermission is true.
            if (excludeMetadataPermission)
            {
                foreach (Group aGroup in groups)
                {
                    System.Collections.ObjectModel.Collection<ModelPrivilege> tempModelPrivileges = new System.Collections.ObjectModel.Collection<ModelPrivilege> { };

                    // Exclude model privileges for Metadata (internal id = 1).
                    foreach (ModelPrivilege aPrivilege in aGroup.SecurityPrivilege.ModelPrivileges)
                    {
                        if (aPrivilege.ModelId.InternalId != 1)
                        {
                            tempModelPrivileges.Add(aPrivilege);
                        }
                    }

                    aGroup.SecurityPrivilege.ModelPrivileges = tempModelPrivileges;
                }
            }

            // Set users and groups objects to securityInformation.
            SecurityInformation securityInformation = new SecurityInformation();
            securityInformation.Users = users;
            securityInformation.Groups = groups;

            // Serialization.
            XmlSerializer serializer = new XmlSerializer(typeof(SecurityInformation));

            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                XmlDictionaryWriter xmlWriter = XmlDictionaryWriter.CreateBinaryWriter(fs);

                // Serializes the security information.
                serializer.Serialize(xmlWriter, securityInformation);
                fs.Flush();
            }
        }
Exemplo n.º 2
0
        // Gets security information and exports it into the files.
        // excludeMetadataPermission indicates if model privileges for Metadata are exluded.
        private static void ExportSecurityInformation(bool excludeMetadataPermission, string fileName)
        {
            // Gets security information.
            SecurityPrincipalsGetRequest principalGetRequest = new SecurityPrincipalsGetRequest();

            principalGetRequest.Criteria                          = new SecurityPrincipalsCriteria();
            principalGetRequest.Criteria.All                      = true;
            principalGetRequest.Criteria.Type                     = PrincipalType.UserAccount;
            principalGetRequest.Criteria.ResultType               = ResultType.Details;
            principalGetRequest.Criteria.SecurityResolutionType   = SecurityResolutionType.Users;
            principalGetRequest.Criteria.ModelPrivilege           = ResultType.Details;
            principalGetRequest.Criteria.FunctionPrivilege        = ResultType.Details;
            principalGetRequest.Criteria.HierarchyMemberPrivilege = ResultType.Details;

            // Gets the security principals for all the users.
            SecurityPrincipalsGetResponse principalGetResponse = clientProxy.SecurityPrincipalsGet(principalGetRequest);

            HandleOperationErrors(principalGetResponse.OperationResult);

            System.Collections.ObjectModel.Collection <User> users = principalGetResponse.Principals.Users;

            // Exclude model privileges for Metadata when excludeMetadataPermission is true.
            if (excludeMetadataPermission)
            {
                foreach (User anUser in users)
                {
                    System.Collections.ObjectModel.Collection <ModelPrivilege> tempModelPrivileges = new System.Collections.ObjectModel.Collection <ModelPrivilege> {
                    };

                    // Exclude model privileges for Metadata (internal id = 1).
                    foreach (ModelPrivilege aPrivilege in anUser.SecurityPrivilege.ModelPrivileges)
                    {
                        if (aPrivilege.ModelId.InternalId != 1)
                        {
                            tempModelPrivileges.Add(aPrivilege);
                        }
                    }

                    anUser.SecurityPrivilege.ModelPrivileges = tempModelPrivileges;
                }
            }

            principalGetRequest.Criteria.Type = PrincipalType.Group;
            principalGetRequest.Criteria.SecurityResolutionType = SecurityResolutionType.UserAndGroup;

            // Gets the security principals for all the groups.
            SecurityPrincipalsGetResponse principalGetGroupResponse = clientProxy.SecurityPrincipalsGet(principalGetRequest);

            HandleOperationErrors(principalGetGroupResponse.OperationResult);

            System.Collections.ObjectModel.Collection <Group> groups = principalGetGroupResponse.Principals.Groups;

            // Exclude model privileges for Metadata when excludeMetadataPermission is true.
            if (excludeMetadataPermission)
            {
                foreach (Group aGroup in groups)
                {
                    System.Collections.ObjectModel.Collection <ModelPrivilege> tempModelPrivileges = new System.Collections.ObjectModel.Collection <ModelPrivilege> {
                    };

                    // Exclude model privileges for Metadata (internal id = 1).
                    foreach (ModelPrivilege aPrivilege in aGroup.SecurityPrivilege.ModelPrivileges)
                    {
                        if (aPrivilege.ModelId.InternalId != 1)
                        {
                            tempModelPrivileges.Add(aPrivilege);
                        }
                    }

                    aGroup.SecurityPrivilege.ModelPrivileges = tempModelPrivileges;
                }
            }

            // Set users and groups objects to securityInformation.
            SecurityInformation securityInformation = new SecurityInformation();

            securityInformation.Users  = users;
            securityInformation.Groups = groups;

            // Serialization.
            XmlSerializer serializer = new XmlSerializer(typeof(SecurityInformation));

            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                XmlDictionaryWriter xmlWriter = XmlDictionaryWriter.CreateBinaryWriter(fs);

                // Serializes the security information.
                serializer.Serialize(xmlWriter, securityInformation);
                fs.Flush();
            }
        }