public dynamic CreateModel(ActivityStreamRecord item)
        {
            var session = this.transactionManager.GetSession();

            session.Evict(item);
            item.CreationDateTime = CRMHelper.SetSiteTimeZone(this.services.WorkContext, item.CreationDateTime);

            dynamic itemModel   = new ExpandoObject();
            dynamic description = JObject.Parse(item.Description);

            itemModel.Changes = description.Changes != null ?
                                ((JArray)description.Changes).Select(c => c.ToString()).ToList() :
                                new List <string>();
            RouteValueDictionary route = null;

            if (description.Route != null)
            {
                route = new RouteValueDictionary();
                foreach (var routeItem in description.Route)
                {
                    route.Add(routeItem.Name, routeItem.Value.ToString());
                }
            }
            else if (item.RelatedContent != null && description.CreateLinkToTheChange == true)
            {
                route = new RouteValueDictionary();
                route.Add("controller", "Item");
                route.Add("Action", "Display");
                route.Add("id", item.RelatedContent.Id);
                route.Add("area", "Contents");
            }

            itemModel.Route              = route;
            itemModel.Id                 = item.RelatedContent.Id;
            itemModel.ActivityStreamId   = item.Id;
            itemModel.ContentDescription = description.ContentDescription;
            itemModel.DateTime           = item.CreationDateTime;

            if (item.User != null)
            {
                var user = this.basicDataService.GetOperatorOrCustomerUser(item.User.Id);
                itemModel.User         = user;
                itemModel.UserId       = item.User.Id;
                itemModel.UserFullName = user != null?CRMHelper.GetFullNameOfUser(user) : item.User.UserName;
            }
            else
            {
                var user = _memebershipService.GetUser(services.WorkContext.CurrentSite.SuperUser);
                itemModel.User         = user;
                itemModel.UserId       = user.Id;
                itemModel.UserFullName = T("System").Text;
            }

            return(itemModel);
        }
Beispiel #2
0
        public ContentItemSetPermissionsViewModel CreateModel()
        {
            var teams         = this.basicDataService.GetTeams().ToList();
            var businessUnits = this.basicDataService.GetBusinessUnits().ToList();

            var model = new ContentItemSetPermissionsViewModel();

            // fill businessUnits
            foreach (var businessUnit in businessUnits)
            {
                var businessUnitPart  = businessUnit.As <BusinessUnitPart>();
                var businessUnitModel = new BusinessUnitViewModel
                {
                    BusinessUnitId = businessUnitPart.Id,
                    Name           = businessUnitPart.Name,
                    Description    = businessUnitPart.Description,
                };

                foreach (var team in teams)
                {
                    var teamPart = team.As <TeamPart>();

                    if (teamPart.BusinessUnit != null && teamPart.BusinessUnit.Id == businessUnitPart.Id)
                    {
                        var teamViewModel = new TeamViewModel
                        {
                            TeamId         = teamPart.Id,
                            BusinessUnitId = teamPart.BusinessUnit != null ? (int?)teamPart.BusinessUnit.Id : null,
                            Name           = teamPart.Name,
                            Description    = teamPart.Description
                        };

                        model.Teams.Add(teamViewModel);
                        businessUnitModel.Teams.Add(teamViewModel);
                    }
                }

                model.BusinessUnits.Add(businessUnitModel);
            }

            // get users with operator permission
            var users = this.basicDataService.GetOperators().ToList();

            foreach (var user in users)
            {
                model.Users.Add(new SelectListItem
                {
                    Value = user.Id.ToString(CultureInfo.InvariantCulture),
                    Text  = CRMHelper.GetFullNameOfUser(user.As <UserPart>())
                });
            }

            return(model);
        }
Beispiel #3
0
        public int GetCustomersCount(string searchPhrase)
        {
            var operatorsAndCustomers = this.GetOperatorAndCustomers();

            // cache values
            if (false && operatorsAndCustomers != null)
            {
                var operators = this.GetOperators();
                var customers = operators.Count() > 0 ?
                                operatorsAndCustomers.Where(c => !operators.Any(d => d.Id == c.Id)).ToList() :
                                operatorsAndCustomers;

                if (!string.IsNullOrEmpty(searchPhrase))
                {
                    searchPhrase = searchPhrase.ToLower(CultureInfo.InvariantCulture);
                    customers    = customers.Where(c =>
                                                   c.Email.ToLower().Contains(searchPhrase) ||
                                                   c.UserName.ToLower().Contains(searchPhrase) ||
                                                   CRMHelper.GetFullNameOfUser(c).ToLower().Contains(searchPhrase)).ToList();
                }

                return(customers.Count);
            }
            else
            {
                var temp = this.rolesPermissionsRepository.Table.Where(c =>
                                                                       c.Permission.Name == Permissions.CustomerPermission.Name &&
                                                                       c.Permission.FeatureName == "Orchard.CRM.Core").ToList();

                var customerRoles = temp.ConvertAll(c => c.Role.Id).ToArray();

                searchPhrase = searchPhrase != null?searchPhrase.ToLower(CultureInfo.InvariantCulture) : string.Empty;

                var customers = (from userRole in this.userRolesRepository.Table
                                 join
                                 user in this.userRepository.Table
                                 on userRole.UserId equals user.Id
                                 join f in fieldRepository.Table
                                 on user.Id equals f.ContentItemRecord.Id

                                 where customerRoles.Contains(userRole.Role.Id) &&
                                 (searchPhrase == null ||
                                  user.UserName.Contains(searchPhrase) ||
                                  user.Email.Contains(searchPhrase) ||
                                  f.StringFieldIndexRecords.Any(d => d.Value.Contains(searchPhrase) && d.PropertyName == "User.FullName")
                                 )
                                 select user);

                return(customers.Count());
            }
        }
Beispiel #4
0
        public IEnumerable <IUser> GetCustomers(string searchPhrase, int pageId, int pageSize, UsersOrderViewModel orderBy, bool decsending)
        {
            if (pageId <= 0)
            {
                pageId = 1;
            }
            var operatorsAndCustomers = this.GetOperatorAndCustomers();

            // cache values
            if (false && operatorsAndCustomers != null)
            {
                var operators = this.GetOperators();
                var customers = operators.Count() > 0 ?
                                operatorsAndCustomers.Where(c => !operators.Any(d => d.Id == c.Id)).ToList() :
                                operatorsAndCustomers;

                // TODO: fix the issue of checking user tags
                if (!string.IsNullOrEmpty(searchPhrase))
                {
                    searchPhrase = searchPhrase.ToLower(CultureInfo.InvariantCulture);
                    customers    = customers.Where(c =>
                                                   c.Email.ToLower().Contains(searchPhrase) ||
                                                   c.UserName.ToLower().Contains(searchPhrase) ||
                                                   CRMHelper.GetFullNameOfUser(c).ToLower().Contains(searchPhrase)).ToList();
                }

                switch (orderBy)
                {
                case UsersOrderViewModel.Default:
                    customers = decsending ? customers.OrderBy(c => c.Id).ToList() : customers.OrderByDescending(c => c.Id).ToList();
                    break;

                case UsersOrderViewModel.Email:
                    customers = decsending ? customers.OrderBy(c => c.Email).ToList() : customers.OrderByDescending(c => c.Email).ToList();
                    break;

                case UsersOrderViewModel.Username:
                    customers = decsending ? customers.OrderBy(c => c.UserName).ToList() : customers.OrderByDescending(c => c.UserName).ToList();
                    break;
                }

                return(customers.Skip((pageId - 1) * pageSize).Take(pageSize));
            }
            else
            {
                var temp = this.rolesPermissionsRepository.Table.Where(c =>
                                                                       c.Permission.Name == Permissions.CustomerPermission.Name &&
                                                                       c.Permission.FeatureName == "Orchard.CRM.Core").ToList();

                var customerRoles = temp.ConvertAll(c => c.Role.Id).ToArray();

                searchPhrase = searchPhrase != null?searchPhrase.ToLower(CultureInfo.InvariantCulture) : string.Empty;

                var customers = (from userRole in this.userRolesRepository.Table
                                 join
                                 user in this.userRepository.Table
                                 on userRole.UserId equals user.Id
                                 join f in fieldRepository.Table
                                 on user.Id equals f.ContentItemRecord.Id

                                 where customerRoles.Contains(userRole.Role.Id) &&
                                 (searchPhrase == null ||
                                  user.UserName.Contains(searchPhrase) ||
                                  user.Email.Contains(searchPhrase) ||
                                  f.StringFieldIndexRecords.Any(d => d.Value.Contains(searchPhrase) && (d.PropertyName == "User.FullName" || d.PropertyName == "User.Tags"))
                                 )
                                 select user);

                switch (orderBy)
                {
                case UsersOrderViewModel.Default:
                    customers = decsending ? customers.OrderBy(c => c.Id) : customers.OrderByDescending(c => c.Id);
                    break;

                case UsersOrderViewModel.Email:
                    customers = decsending ? customers.OrderBy(c => c.Email) : customers.OrderByDescending(c => c.Email);
                    break;

                case UsersOrderViewModel.Username:
                    customers = decsending ? customers.OrderBy(c => c.UserName) : customers.OrderByDescending(c => c.UserName);
                    break;
                }

                List <int> ids = customers.Skip((pageId - 1) * pageSize).Take(pageSize).Select(c => c.Id).ToList();

                return(this.contentManager.GetMany <UserPart>(ids, VersionOptions.Published, new QueryHints()).Select(c => c.As <IUser>()).ToList());
            }
        }
Beispiel #5
0
        public void FillPermissions(ContentItemSetPermissionsViewModel model, IEnumerable <ContentItem> contentItems, bool onlyAddOwnerPermissions)
        {
            var businessUnits = this.basicDataService.GetBusinessUnits().ToList();
            var teams         = this.basicDataService.GetTeams().ToList();

            if (contentItems != null && contentItems.Count() > 0)
            {
                foreach (var contentItem in contentItems)
                {
                    ContentItemSetPermissionsViewModel.ContentItemPermissionsModel contentItemPermissionModel = new ContentItemSetPermissionsViewModel.ContentItemPermissionsModel();
                    contentItemPermissionModel.ContentItem = contentItem;
                    model.ContentItems.Add(contentItemPermissionModel);

                    var contentPermissionPart = contentItem.As <ContentItemPermissionPart>();
                    if (contentPermissionPart == null)
                    {
                        throw new Security.OrchardSecurityException(T("The ContentItem doesn't have any ContentItemPermissionPart part."));
                    }

                    var allPermissionRecords = contentPermissionPart.Record.Items;

                    if (allPermissionRecords == null)
                    {
                        allPermissionRecords = new List <ContentItemPermissionDetailRecord>();
                    }

                    contentItemPermissionModel.IsCurrentUserOwner = this.crmContentOwnershipService.CurrentUserIsContentItemAssignee(contentItem);

                    // operator permission can access unassigned items
                    contentItemPermissionModel.CurrentUserHasRightToChangePermissions = this.crmContentOwnershipService.CurrentUserCanChangePermission(contentItem, new ModelStateDictionary());

                    if (onlyAddOwnerPermissions)
                    {
                        allPermissionRecords = allPermissionRecords.Where(c => c.AccessType == ContentItemPermissionAccessTypes.Assignee).ToList();
                    }

                    // fill the list of current permission items
                    foreach (var itemPermission in allPermissionRecords)
                    {
                        var itemModel = new ContentItemSetPermissionsViewModel.ItemPermissionViewModel
                        {
                            Id             = itemPermission.Id,
                            ContentItemId  = contentItem.Id,
                            AccessType     = itemPermission.AccessType,
                            BusinessUnitId = itemPermission.BusinessUnit != null ? (int?)itemPermission.BusinessUnit.Id : null,
                            TeamId         = itemPermission.Team != null ? (int?)itemPermission.Team.Id : null,
                            UserId         = itemPermission.User != null ? (int?)itemPermission.User.Id : null,
                        };

                        if (itemPermission.User != null)
                        {
                            var user = this.basicDataService.GetOperatorOrCustomerUser(itemPermission.User.Id);
                            itemModel.Name = CRMHelper.GetFullNameOfUser(user);
                        }
                        else if (itemPermission.Team != null)
                        {
                            var team = teams.FirstOrDefault(c => c.Id == itemPermission.Team.Id);
                            itemModel.Name = team != null?team.As <TeamPart>().Name : string.Empty;
                        }
                        else if (itemPermission.BusinessUnit != null)
                        {
                            var businessUnit = businessUnits.FirstOrDefault(c => c.Id == itemPermission.BusinessUnit.Id);
                            if (businessUnit != null && businessUnit.As <BusinessUnitPart>() != null)
                            {
                                itemModel.Name = businessUnit.As <BusinessUnitPart>().Name;
                            }
                        }

                        // checked the business Unit in the list
                        if (itemModel.BusinessUnitId.HasValue)
                        {
                            var businessUnit = model.BusinessUnits.FirstOrDefault(c => c.BusinessUnitId == itemModel.BusinessUnitId.Value);
                            if (businessUnit != null)
                            {
                                businessUnit.Checked = true;
                            }
                        }

                        // checked the team in the list
                        if (itemModel.TeamId.HasValue)
                        {
                            var team = model.Teams.FirstOrDefault(c => c.TeamId == itemModel.TeamId.Value);
                            if (team != null)
                            {
                                team.Checked = true;
                            }
                        }

                        // checked the user in the list
                        if (itemModel.UserId.HasValue)
                        {
                            var user = model.Users.FirstOrDefault(c => c.Value == itemModel.UserId.Value.ToString(CultureInfo.InvariantCulture));
                            if (user != null)
                            {
                                user.Selected = true;
                            }
                        }

                        contentItemPermissionModel.CurrentPermissions.Add(itemModel);
                    }
                }
            }
        }