Пример #1
0
        public static RequestResult GetCreateFormModel(Account account)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                requestResult = GetWebPermissions();

                if (requestResult.IsSuccess)
                {
                    requestResult.ReturnData(new CreateFormModel()
                    {
                        AncestorOrganizationId = OrganizationDataAccessor.GetAncestorOrganizationId(account.OrganizationId).ToString(),
                        WebPermissionFunction  = new WebPermissionFunctionModel()
                        {
                            WebPermissions = requestResult.Data as List <WebPermissionModel>
                        }
                    });
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                //throw;
            }

            return(requestResult);
        }
Пример #2
0
        public static RequestResult GetDetailViewModel(string pId, Account account)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                using (CFContext context = new CFContext())
                {
                    var person = context.People.First(p => p.LoginId == pId);

                    requestResult.ReturnData(new DetailViewModel()
                    {
                        Permission                 = account.OrganizationPermission(person.OrganizationId),
                        OrganizationId             = person.OrganizationId.ToString(),
                        ParentOrganizationFullName = OrganizationDataAccessor.GetOrganizationFullName(person.OrganizationId),
                        PId            = person.LoginId,
                        Name           = person.Name,
                        EMail          = person.Email,
                        Title          = person.Title,
                        IsMobilePerson = person.IsMobilePerson,
                        RoleNames      = person.Roles.Select(r => r.Name).ToList()
                    });
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                //throw;
            }

            return(requestResult);
        }
Пример #3
0
        public static RequestResult GetCreateFormModel(Guid organizationId)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                using (CFContext context = new CFContext())
                {
                    requestResult.ReturnData(new CreateFormModel()
                    {
                        OrganizationId             = organizationId.ToString(),
                        ParentOrganizationFullName = OrganizationDataAccessor.GetOrganizationFullName(organizationId),
                        RoleModels = context.Roles.Select(r => new RoleModel {
                            RoleId = r.RoleId, RoleName = r.Name
                        }).OrderBy(r => r.RoleId).ToList()
                    });
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                //throw;
            }

            return(requestResult);
        }
Пример #4
0
        public static RequestResult GetCopyFormModel(string pId)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                using (CFContext context = new CFContext())
                {
                    var person = context.People.First(p => p.LoginId == pId);

                    requestResult.ReturnData(new CreateFormModel()
                    {
                        OrganizationId             = person.Organization.ToString(),
                        ParentOrganizationFullName = OrganizationDataAccessor.GetOrganizationFullName(person.OrganizationId),
                        RoleModels = context.Roles.Select(r => new RoleModel {
                            RoleId = r.RoleId, RoleName = r.Name
                        }).OrderBy(r => r.RoleId).ToList(),
                        FormInput = new FormInput()
                        {
                            Title          = person.Title,
                            IsMobilePerson = person.IsMobilePerson
                        }
                    });
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                throw;
            }

            return(requestResult);
        }
Пример #5
0
        public static RequestResult AddPerson(List <PersonModel> people, List <string> selectedList)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                string[] seperator = new string[] { Define.Seperator };

                using (CFContext context = new CFContext())
                {
                    foreach (string selected in selectedList)
                    {
                        string[] temp = selected.Split(seperator, StringSplitOptions.None);

                        var organizationId = temp[0];
                        var pId            = temp[1];

                        if (!string.IsNullOrEmpty(pId))
                        {
                            if (!people.Any(p => p.PId == pId))
                            {
                                //var person = context.People.First(p => p.LoginId == pId);
                                people.Add(context.People.Where(p => p.LoginId == pId).Select(p => new PersonModel {
                                    PId = pId, PersonName = p.Name, OrganizationName = p.Organization.Name
                                }).First());
                            }
                        }
                        else
                        {
                            var organizations = OrganizationDataAccessor.GetDownStreamOrganizations(new Guid(organizationId), true);

                            var newPeople = context.People.Where(p => organizations.Contains(p.OrganizationId)).ToList();
                            foreach (var person in newPeople)
                            {
                                if (!people.Any(p => p.PId == person.LoginId))
                                {
                                    people.Add(new PersonModel {
                                        PId = person.LoginId, PersonName = person.Name, OrganizationName = person.Organization.Name
                                    });
                                }
                            }
                        }
                    }
                }

                requestResult.ReturnData(people.OrderBy(p => p.OrganizationName).ThenBy(p => p.PId).ToList());
            }
            catch (Exception e)
            {
                var error = new Error(MethodInfo.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                //throw;
            }

            return(requestResult);
        }
Пример #6
0
        public static RequestResult GetEditFormModel(string roleId, Account account)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                requestResult = GetWebPermissions();

                if (requestResult.IsSuccess)
                {
                    using (CFContext context = new CFContext())
                    {
                        var role = context.Roles.First(r => r.RoleId == roleId);
                        requestResult.ReturnData(new EditFormModel()
                        {
                            AncestorOrganizationId = OrganizationDataAccessor.GetAncestorOrganizationId(account.OrganizationId).ToString(),
                            RoleId = role.RoleId,
                            WebPermissionFunction = new WebPermissionFunctionModel()
                            {
                                WebPermissions             = requestResult.Data as List <WebPermissionModel>,
                                RoleWebPermissionFunctions = context.RolePermissionFunctions.Where(wpwf => wpwf.RoleId == role.RoleId).Select(wpwf => new Models.Role.RoleWebPermissionFunctionModel {
                                    RoleId = wpwf.RoleId, WebPermissionId = wpwf.WebPermissionId, WebFunctionId = wpwf.WebFunctionId
                                }).ToList()
                            },
                            FormInput = new FormInput()
                            {
                                RoleId = role.RoleId,
                                Name   = role.Name
                            },
                            People = (from p in role.People
                                      join o in context.Organizations
                                      on p.OrganizationId equals o.OrganizationId
                                      select new Models.Role.PersonModel
                            {
                                OrganizationName = o.Name,
                                PId = p.LoginId,
                                PersonName = p.Name
                            }).OrderBy(x => x.OrganizationName).ThenBy(x => x.PId).ToList()
                        });
                    }
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                //throw;
            }

            return(requestResult);
        }
Пример #7
0
        public static RequestResult Query(QueryParameters queryParameters, Account account)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                using (CFContext context = new CFContext())
                {
                    var downStreamOrganizations = OrganizationDataAccessor.GetDownStreamOrganizationIds(new Guid(queryParameters.OrganizationId), true);

                    var query = (from p in context.People
                                 join o in context.Organizations
                                 on p.OrganizationId equals o.OrganizationId
                                 where downStreamOrganizations.Contains(p.OrganizationId) && account.QueryableOrganizationIds.Contains(p.OrganizationId)
                                 select new
                    {
                        Person = p,
                        OrganizationName = o.Name
                    }).AsQueryable();
                    if (!string.IsNullOrEmpty(queryParameters.Keyword))
                    {
                        query = query.Where(x => x.Person.Title.Contains(queryParameters.Keyword) || x.Person.LoginId.Contains(queryParameters.Keyword) || x.Person.Name.Contains(queryParameters.Keyword));
                    }

                    var organization = OrganizationDataAccessor.GetOrganization(new Guid(queryParameters.OrganizationId));

                    var items = query.ToList();

                    var model = new GridViewModel()
                    {
                        Permission           = account.OrganizationPermission(new Guid(queryParameters.OrganizationId)),
                        OrganizationId       = queryParameters.OrganizationId,
                        OrganizationName     = organization.Name,
                        FullOrganizationName = organization.FullName,
                        Items = items.Select(x => new GridItem()
                        {
                            Permission       = account.OrganizationPermission(x.Person.OrganizationId),
                            OrganizationName = x.OrganizationName,
                            Title            = x.Person.Title,
                            Id             = x.Person.LoginId,
                            Name           = x.Person.Name,
                            Email          = x.Person.Email,
                            IsMobilePerson = x.Person.IsMobilePerson,
                            RoleNameList   = x.Person.Roles.Select(r => r.Name).ToList()
                        }).OrderBy(x => x.OrganizationName).ThenBy(x => x.Title).ThenBy(x => x.Id).ToList()
                    };

                    var ancestorOrganizationId = OrganizationDataAccessor.GetAncestorOrganizationId(new Guid(queryParameters.OrganizationId));

                    var downStreams = OrganizationDataAccessor.GetDownStreamOrganizations(ancestorOrganizationId, true);

                    foreach (var downStream in downStreams)
                    {
                        if (account.EditableOrganizationIds.Any(x => x == downStream))
                        {
                            model.MoveToTargets.Add(new MoveToTarget()
                            {
                                Id        = downStream,
                                Name      = OrganizationDataAccessor.GetOrganizationFullName(downStream),
                                Direction = Define.EnumMoveDirection.Down
                            });
                        }
                    }

                    model.MoveToTargets = model.MoveToTargets.OrderBy(x => x.Name).ToList();

                    requestResult.ReturnData(model);
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                //throw;
            }

            return(requestResult);
        }
Пример #8
0
        public static RequestResult Create(CreateFormModel model)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                using (CFContext context = new CFContext())
                {
                    var exists = context.People.FirstOrDefault(p => p.LoginId == model.FormInput.Id);

                    if (exists == null)
                    {
                        bool exceedsLimit = false;

                        var upStreamOrganizationIds = OrganizationDataAccessor.GetUpStreamOrganizationIds(new Guid(model.OrganizationId), true);

                        PopulationLimit populationLimit = null;

                        foreach (var item in Config.PopulationLimits)
                        {
                            if (upStreamOrganizationIds.Contains(item.OrganizationId))
                            {
                                populationLimit = item;
                                break;
                            }
                        }
                        if (populationLimit != null)
                        {
                            var organization            = OrganizationDataAccessor.GetOrganization(populationLimit.OrganizationId);
                            var downStreamOrganizations = OrganizationDataAccessor.GetDownStreamOrganizations(populationLimit.OrganizationId, true);
                            var people = context.People.Where(p => downStreamOrganizations.Contains(p.OrganizationId)).ToList();
                            if (people.Count + 1 > populationLimit.NumberOfPeople)
                            {
                                exceedsLimit = true;
                                requestResult.ReturnFailedMessage(string.Format(Resources.Resource.ExceedsPopulationLimit, organization.Name, populationLimit.NumberOfPeople));
                            }
                            else
                            {
                                if (model.FormInput.IsMobilePerson && people.Count(x => x.IsMobilePerson) + 1 > populationLimit.NumberOfMobilePeople)
                                {
                                    exceedsLimit = true;
                                    requestResult.ReturnFailedMessage(string.Format(Resources.Resource.ExceedsMobilePopulationLimit, organization.Name, populationLimit.NumberOfMobilePeople));
                                }
                            }
                        }
                        if (!exceedsLimit)
                        {
                            var roles = context.Roles.Where(r => model.FormInput.RoleIdsString.Contains(r.RoleId)).ToList();
                            context.People.Add(new CF.Models.Person()
                            {
                                OrganizationId = new Guid(model.OrganizationId),
                                LoginId        = model.FormInput.Id,
                                Name           = model.FormInput.Name,
                                Password       = model.FormInput.Id,
                                Title          = model.FormInput.Title,
                                Email          = model.FormInput.EMail,
                                IsMobilePerson = model.FormInput.IsMobilePerson,
                                LastModifyTime = DateTime.Now,
                                Roles          = roles
                            });

                            context.SaveChanges();
                            requestResult.ReturnData(model.FormInput.Id, string.Format("{0} {1} {2}", Resources.Resource.Create, Resources.Resource.Person, Resources.Resource.Success));
                        }
                    }
                    else
                    {
                        requestResult.ReturnFailedMessage(string.Format("{0} {1}", Resources.Resource.PId, Resources.Resource.Exists));
                    }
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                //throw;
            }

            return(requestResult);
        }
Пример #9
0
        public static RequestResult Edit(EditFormModel editFormModel)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                using (CFContext context = new CFContext())
                {
                    var person = context.People.Include("Roles").First(x => x.LoginId == editFormModel.PId);

                    bool exceedsLimit = false;

                    if (editFormModel.FormInput.IsMobilePerson)
                    {
                        var upStreamOrganizationIds = OrganizationDataAccessor.GetUpStreamOrganizationIds(person.OrganizationId, true);

                        PopulationLimit populationLimit = null;

                        foreach (var item in Config.PopulationLimits)
                        {
                            if (upStreamOrganizationIds.Contains(item.OrganizationId))
                            {
                                populationLimit = item;

                                break;
                            }
                        }

                        if (populationLimit != null)
                        {
                            var organization = OrganizationDataAccessor.GetOrganization(populationLimit.OrganizationId);

                            var downStreamOrganizations = OrganizationDataAccessor.GetDownStreamOrganizations(populationLimit.OrganizationId, true);

                            var mobilePeople = context.People.Where(x => x.LoginId != person.LoginId && x.IsMobilePerson && downStreamOrganizations.Contains(x.OrganizationId)).ToList();

                            if (mobilePeople.Count + 1 > populationLimit.NumberOfMobilePeople)
                            {
                                exceedsLimit = true;

                                requestResult.ReturnFailedMessage(string.Format(Resources.Resource.ExceedsMobilePopulationLimit, organization.Name, populationLimit.NumberOfMobilePeople));
                            }
                        }
                    }

                    if (!exceedsLimit)
                    {
#if !DEBUG
                        using (TransactionScope trans = new TransactionScope())
                        {
#endif
                        #region Person
                        person.Name  = editFormModel.FormInput.Name;
                        person.Title = editFormModel.FormInput.Title;
                        person.Email = editFormModel.FormInput.EMail;
                        //person.UID = editFormModel.FormInput.UID;
                        person.IsMobilePerson = editFormModel.FormInput.IsMobilePerson;
                        person.LastModifyTime = DateTime.Now;

                        context.SaveChanges();
                        #endregion

                        #region PersonRoles
                        #region Delete
                        if (person != null)
                        {
                            person.Roles = new List <CF.Models.Role>();
                            context.SaveChanges();
                        }

                        #endregion

                        #region Insert
                        foreach (var roleId in editFormModel.FormInput.RoleIds)
                        {
                            var role = context.Roles.First(r => r.RoleId == roleId);
                            person.Roles.Add(role);
                        }
                        context.SaveChanges();
                        #endregion
                        #endregion
#if !DEBUG
                        trans.Complete();
                    }
#endif
                        requestResult.ReturnSuccessMessage(string.Format("{0} {1} {2}", Resources.Resource.Edit, Resources.Resource.Person, Resources.Resource.Success));
                    }
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
                //throw;
            }

            return(requestResult);
        }
Пример #10
0
        public static RequestResult GetAccount(List <Models.Shared.Organization> organizations, Person person)
        {
            RequestResult requestResult = new RequestResult();

            try
            {
                using (CFContext context = new CFContext())
                {
                    var newPerson                 = context.People.Include("Roles").Where(p => p.PersonId == person.PersonId).FirstOrDefault();
                    var webPermissionsAll         = new List <WebPermission>();
                    var webFunctionsAll           = new List <WebFunction>();
                    var webPermissionWebFunctions = new List <WebPermissionWebFunctionModel>();
                    var roleIds = new List <string>();

                    foreach (var r in newPerson.Roles)
                    {
                        var webPermissionIds = r.WebPermissionWebFunctions.Select(wpwf => wpwf.WebPermissionId).ToList();
                        foreach (var webPermissionId in webPermissionIds)
                        {
                            var webPermission            = context.WebPermissions.Where(wp => wp.WebPermissionId == webPermissionId).FirstOrDefault();
                            var webPermissionWebFunction = r.WebPermissionWebFunctions.Select(wpwf => new WebPermissionWebFunctionModel
                            {
                                WebPermissionId = wpwf.WebPermissionId,
                                WebFunctionId   = wpwf.WebFunctionId,
                                Area            = webPermission.Area,
                                Controller      = webPermission.Controller,
                                Action          = webPermission.Action
                            }).ToList();
                            webPermissionWebFunctions.AddRange(webPermissionWebFunction);
                        }


                        //var personPermissions = context.WebPermissions.Where(wp => wp.WebPermissionId == webPermissionId).Select(x => new
                        //{
                        //    x.WebPermissionId,
                        //    x.ParentId,
                        //}).ToList();
                    }

                    roleIds = newPerson.Roles.Select(r => r.RoleId).ToList();

                    //foreach (var role in newPerson.Roles)
                    //{
                    //    var webPermissionIds = role.WebPermissionWebFunctions.Select(wpwf => wpwf.WebPermissionId);
                    //    var webPermissions = context.WebPermissions.Include("WebPermissionWebFunctions").Where(wp => webPermissionIds.Contains(wp.WebPermissionId) && wp.ParentId != "*");
                    //    webPermissionsAll.AddRange(webPermissions);
                    //}

                    var account = new Account()
                    {
                        Id             = person.LoginId,
                        Name           = person.Name,
                        OrganizationId = person.OrganizationId,
                        //WebPermissions = webPermissionsAll.Distinct(new PropertyCompare<WebPermission>("WebPermissionId")).ToList(),
                        WebPermissionFunctions = webPermissionWebFunctions,
                        RoleIds = roleIds,
                        OrganizationPermissions = OrganizationDataAccessor.GetOrganizationPermissions(person.OrganizationId)
                    };

                    account.RootOrganizationId = OrganizationDataAccessor.GetRootOrganizationId(organizations, account);

                    var personPermissions = (from r in newPerson.Roles
                                             join x in context.RolePermissionFunctions
                                             on r.RoleId equals x.RoleId
                                             join p in context.WebPermissions
                                             on x.WebPermissionId equals p.WebPermissionId
                                             select new
                    {
                        p.WebPermissionId,
                        p.ParentId,
                    }).Distinct().ToList();

                    foreach (var wp in personPermissions)
                    {
                        var parent = new WebPermission();

                        parent = context.WebPermissions.First(p => p.WebPermissionId == wp.ParentId);

                        var permission = context.WebPermissions.First(p => p.WebPermissionId == wp.WebPermissionId);

                        if (parent.ParentId == "*")
                        {
                            var ancestorMenuItem = account.MenuItems.FirstOrDefault(m => m.Id == parent.WebPermissionId);
                            if (ancestorMenuItem == null)
                            {
                                ancestorMenuItem = new MenuItem()
                                {
                                    Id         = parent.WebPermissionId,
                                    Name       = context.WebPermissionNames.Where(wpd => wpd.WebPermissionId == parent.WebPermissionId).ToDictionary(wpd => wpd.Language, wpd => wpd.Name),
                                    Area       = string.Empty,
                                    Controller = string.Empty,
                                    Action     = string.Empty,
                                    Icon       = parent.Icon,
                                    Seq        = parent.Seq
                                };
                                account.MenuItems.Add(ancestorMenuItem);
                            }

                            ancestorMenuItem.SubItemList.Add(new MenuItem()
                            {
                                Id         = permission.WebPermissionId,
                                Name       = context.WebPermissionNames.Where(wpd => wpd.WebPermissionId == permission.WebPermissionId).ToDictionary(wpd => wpd.Language, wpd => wpd.Name),
                                Area       = permission.Area,
                                Controller = permission.Controller,
                                Action     = permission.Action,
                                Icon       = permission.Icon,
                                Seq        = permission.Seq
                            });
                        }
                        else
                        {
                            var ancestor         = context.WebPermissions.First(p => p.WebPermissionId == parent.ParentId);
                            var ancestorMenuItem = account.MenuItems.FirstOrDefault(m => m.Id == ancestor.WebPermissionId);
                            if (ancestorMenuItem == null)
                            {
                                ancestorMenuItem = new MenuItem()
                                {
                                    Id         = ancestor.WebPermissionId,
                                    Name       = context.WebPermissionNames.Where(wpd => wpd.WebPermissionId == ancestor.WebPermissionId).ToDictionary(wpd => wpd.Language, wpd => wpd.Name),
                                    Area       = string.Empty,
                                    Controller = string.Empty,
                                    Action     = string.Empty,
                                    Icon       = ancestor.Icon,
                                    Seq        = ancestor.Seq
                                };

                                account.MenuItems.Add(ancestorMenuItem);
                            }

                            var parentMenuItem = ancestorMenuItem.SubItemList.FirstOrDefault(s => s.Id == parent.WebPermissionId);
                            if (parentMenuItem == null)
                            {
                                parentMenuItem = new MenuItem()
                                {
                                    Id         = parent.WebPermissionId,
                                    Name       = context.WebPermissionNames.Where(wpd => wpd.WebPermissionId == parent.WebPermissionId).ToDictionary(wpd => wpd.Language, wpd => wpd.Name),
                                    Area       = string.Empty,
                                    Controller = string.Empty,
                                    Action     = string.Empty,
                                    Icon       = parent.Icon,
                                    Seq        = parent.Seq
                                };

                                ancestorMenuItem.SubItemList.Add(parentMenuItem);
                            }

                            parentMenuItem.SubItemList.Add(new MenuItem()
                            {
                                Id         = permission.WebPermissionId,
                                Name       = context.WebPermissionNames.Where(wpd => wpd.WebPermissionId == permission.WebPermissionId).ToDictionary(wpd => wpd.Language, wpd => wpd.Name),
                                Area       = permission.Area,
                                Controller = permission.Controller,
                                Action     = permission.Action,
                                Icon       = permission.Icon,
                                Seq        = permission.Seq
                            });
                        }
                    }

                    foreach (var menu in account.MenuItems)
                    {
                        foreach (var item in menu.SubItemList)
                        {
                            if (item.SubItemList != null)
                            {
                                item.SubItemList = item.SubItemList.OrderBy(x => x.Seq).ToList();
                            }
                        }

                        menu.SubItemList = menu.SubItemList.OrderBy(x => x.Seq).ToList();
                    }

                    requestResult.ReturnData(account);
                }
            }
            catch (Exception e)
            {
                var error = new Error(MethodBase.GetCurrentMethod(), e);
                Logger.Log(error);
                requestResult.ReturnError(error);
            }

            return(requestResult);
        }