Beispiel #1
0
        private void btnImportUserProfiles_Click(object sender, EventArgs e)
        {
            Assembly currAssembly = Assembly.GetExecutingAssembly();

            try
            {
                CrmServiceClient crmServiceClient = (CrmServiceClient)Service;

                var env      = crmServiceClient.CrmConnectOrgUriActual.Host.Substring(0, crmServiceClient.CrmConnectOrgUriActual.Host.IndexOf('.'));
                var fileName = Directory.GetCurrentDirectory() + @"\Export\" + env + "_AssignRolesToUsers.json";

                if (crmServiceClient.CrmConnectOrgUriActual.ToString().Contains(env))
                {
                    var repo = new SecurityManager(crmServiceClient);

                    //change to go up 3 folders in deploymentpackage
                    using (StreamReader file = File.OpenText(fileName))
                    {
                        JsonSerializer          serializer = new JsonSerializer();
                        UserSecurityProfileList jsonConfig = (UserSecurityProfileList)serializer.Deserialize(file, typeof(UserSecurityProfileList));

                        foreach (UserSecurityProfile payload in jsonConfig)
                        {
                            repo.ConfigureUser(payload);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failure to process Team security roles: {ex.Message}");
            }
        }
        public string ExportUserSecurity()
        {
            var jsonPayLoad = "";

            try
            {
                var userProfiles = new UserSecurityProfileList();

                foreach (var user in this._users)
                {
                    var disabled = user.GetAttributeValue <bool>("isdisabled");

                    if (!disabled)
                    {
                        //build object
                        var userProfile = new UserSecurityProfile()
                        {
                            emails = new List <string>()
                            {
                                user.GetAttributeValue <string>("domainname")
                            }
                        };

                        userProfile.businessUnit = user.GetAttributeValue <EntityReference>("businessunitid").Name;

                        var userRoles = GetUserSecurityRoles(user.Id);
                        userProfile.roles = userRoles.Select(entity => entity.GetAttributeValue <string>("name")).ToList();
                        userProfile.roles.Sort();

                        //need to exclude from list
                        var userTeams = GetUserTeams(user.Id);
                        userProfile.teams = userTeams.Select(entity => entity.GetAttributeValue <string>("name")).ToList();
                        userProfile.teams.Sort();

                        //need to exclude from list - do.where.select
                        var userQueues = GetUserQueues(user.Id);
                        userProfile.queues = userQueues
                                             .Where(item => item.GetAttributeValue <AliasedValue>("team.name") == null &&
                                                    item.GetAttributeValue <EntityReference>("ownerid").Id != user.Id)
                                             .Select(entity => entity.GetAttributeValue <string>("name")).ToList();
                        userProfile.queues.Sort();

                        if (userProfiles.Where <UserSecurityProfile>(profile =>
                                                                     user.GetAttributeValue <EntityReference>("businessunitid").Name == profile.businessUnit &&
                                                                     userProfile.roles.SequenceEqual(profile.roles) &&
                                                                     userProfile.teams.SequenceEqual(profile.teams) &&
                                                                     userProfile.queues.SequenceEqual(profile.queues))
                            .FirstOrDefault() != null)
                        {
                            userProfiles.Where <UserSecurityProfile>(profile =>
                                                                     user.GetAttributeValue <EntityReference>("businessunitid").Name == profile.businessUnit &&
                                                                     userProfile.roles.SequenceEqual(profile.roles) &&
                                                                     userProfile.teams.SequenceEqual(profile.teams) &&
                                                                     userProfile.queues.SequenceEqual(profile.queues))
                            .FirstOrDefault().emails.Add(userProfile.emails[0]);
                        }
                        else
                        {
                            userProfiles.Add(userProfile);
                        }
                    }
                }

                jsonPayLoad = JsonConvert.SerializeObject(userProfiles);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failure to process Team security roles: {e.Message}");
            }
            return(jsonPayLoad);
        }