public ActionResult GetTree(int? id)
        {
            var ctvm = CreateViewModel<PermissionTreeViewModel>();
            var role_buildingIds = GetUserBuildings(_buildingRepository, _userRepository, CurrentUser.Get().Id);
            if (CurrentUser.Get().IsCompanyManager || CurrentUser.Get().IsCommonUser)
            {
                UserPermissionGroup upg;
                /*
            	var activeUpg =
            		_userPermissionGroupRepository.FindAll().Where(
            			x => x.PermissionIsActive && !x.IsDeleted && x.UserId == CurrentUser.Get().Id).FirstOrDefault();
                */
                var activeUpg =
                _userPermissionGroupRepository.FindByUserId(CurrentUser.Get().Id).Where(
                    x => x.PermissionIsActive && !x.IsDeleted).FirstOrDefault();
                var activeUpgBuildingIds = activeUpg != null ? (from cbo in activeUpg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted && x.Active) select cbo.BuildingObjectId).ToList() : null;
                if (id.HasValue)
                {
                    upg = _userPermissionGroupRepository.FindById(id.Value);
                    //var cupg = _userPermissionGroupRepository.FindAll(x => x.ParentUserPermissionGroupId == id && x.UserId == CurrentUser.Get().Id);
                    var cupg = _userPermissionGroupRepository.FindByUserId(CurrentUser.Get().Id).Where(x => x.ParentUserPermissionGroupId == id);
                    if (CurrentUser.Get().IsCompanyManager && upg.ParentUserPermissionGroupId == null && cupg.Count() >= 1) { ctvm.IsCurrentUserAssignedGroup = true; }
                    else { ctvm.IsCurrentUserAssignedGroup = upg.PermissionIsActive; }
                }
                else
                {
                    if (_userPermissionGroupRepository.FindByUserId(CurrentUser.Get().Id).Where(x => !x.IsDeleted && x.IsOriginal == true).ToList().Count() == 0) return null;
                    upg = _userPermissionGroupRepository.FindByUserId(CurrentUser.Get().Id).Where(x => !x.IsDeleted && x.IsOriginal == true && x.PermissionIsActive == true).FirstOrDefault();
                    id = upg.Id;
                    ctvm.IsGroupExist = false;
                    ctvm.IsCurrentUserAssignedGroup = false;
                }

                ctvm.IsOriginal = upg.IsOriginal;
                List<int> companyBuildingObjectsIds = (from cbo in upg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted && (activeUpg == null || activeUpgBuildingIds.Contains(x.BuildingObjectId) || x.Active)) select cbo.BuildingObjectId).ToList();
                if (activeUpgBuildingIds != null)
                {
                    foreach (var actUpgBuildingId in
                        activeUpgBuildingIds.Where(actUpgBuildingId => !companyBuildingObjectsIds.Contains(actUpgBuildingId)))
                    {
                        companyBuildingObjectsIds.Add(actUpgBuildingId);
                    }
                }

                var companyBuildingObjects = _buildingObjectRepository.FindAll(x => !x.IsDeleted && /*role_buildingIds.Contains(x.BuildingId)
                    && */companyBuildingObjectsIds.Contains(x.Id));

                // role_buildingIds.Contains(x.BuildingId) not in need
                List<int> floorsId = (from cbo in companyBuildingObjects.Where(x => x.ParentObjectId.HasValue) select cbo.ParentObjectId.Value).ToList();
                var companyFloorObjects = _buildingObjectRepository.FindAll(x => !x.IsDeleted && floorsId.Contains(x.Id));
                ctvm.Buildings =
                    (from b in
                         (from cbo in companyBuildingObjects select cbo.Building)
                     where !b.IsDeleted
                     select
                         new Node
                         {
                             ParentId = b.LocationId,
                             MyId = b.Id,
                             Name = b.Name
                         }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Towns =
                    (from t in
                         (from cbo in companyBuildingObjects select cbo.Building.Location)
                     where !t.IsDeleted
                     select
                         new Node
                         {
                             ParentId = t.CountryId,
                             MyId = t.Id,
                             Name = t.Name
                         }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Countries =
                    (from c in
                         (from cbo in companyBuildingObjects select cbo.Building.Location.Country).OrderBy(cc => cc.Name)
                     where !c.IsDeleted
                     select
                         new Node
                         {
                             ParentId = 0,
                             MyId = c.Id,
                             Name = c.Name
                         }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Floors =
                    (from f in companyFloorObjects
                     where f.TypeId == (int)BuildingObjectTypes.Floor
                     select
                         new Node
                         {
                             ParentId = f.BuildingId,
                             MyId = f.Id,
                             Name = f.Description,
                             Comment = f.Comment
                         }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Objects =
                    (from o in companyBuildingObjects
                     where o.ParentObjectId.HasValue && (o.TypeId == (int)BuildingObjectTypes.Door || o.TypeId == (int)BuildingObjectTypes.Lift || o.TypeId == (int)BuildingObjectTypes.Room)
                     orderby o.TypeId descending
                     select
                         new Node
                         {
                             ParentId = o.ParentObjectId.Value,
                             MyId = o.Id,
                             Name = o.ObjectNr.HasValue ? "#" + o.ObjectNr + " " + o.Description : o.Description,
                             Comment = o.Comment,
                             IsDefaultTimeZone = _userPermissionGroupService.IsDefaultUserTimeZone(o.Id, id.Value),
                             IsRoom = o.TypeId == (int)BuildingObjectTypes.Room ? 1 : 0,
                             StatusIcon = o.StatusIconId.HasValue ? String.Format("../../img/status/{0}.ico", o.StatusIconId) : String.Empty
                         }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.ActiveObjectIds = _userPermissionGroupService.GetUserBuildingObjectIds(id.Value);
                foreach (var obn in ctvm.Objects)
                {
                    if (obn.IsRoom == 1)
                    {
                        //1 illi 08.05.2018 UserPermissionGroupTimeZone upgtz = _userPermissionGroupTimeZoneRepository.FindAll(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();
                        int idd = id.GetValueOrDefault();
                        UserPermissionGroupTimeZone upgtz = _userPermissionGroupTimeZoneRepository.FindByPGId(idd).Where(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();

                        obn.IsArming = upgtz == null ? true : upgtz.IsArming;
                        obn.IsDefaultArming = upgtz == null ? true : upgtz.IsDefaultArming;
                        obn.IsDisarming = upgtz == null ? true : upgtz.IsDisarming;
                        obn.IsDefaultDisarming = upgtz == null ? true : upgtz.IsDefaultDisarming;
                    }
                }
                ctvm.Towns = ctvm.Towns.Where(town => ctvm.Buildings.Any(b => b.ParentId == town.MyId));
                ctvm.Countries = ctvm.Countries.Where(country => ctvm.Towns.Any(town => town.ParentId == country.MyId));
            }
            else
            if (!(CurrentUser.Get().IsCompanyManager || CurrentUser.Get().IsCommonUser))
            {
                IEnumerable<Building> userBuildings;
                if (CurrentUser.Get().IsSuperAdmin)
                {
                    userBuildings = _buildingRepository.FindAll(x => !x.IsDeleted).ToList();
                }
                else
                if (CurrentUser.Get().IsBuildingAdmin)
                {
                    userBuildings = _buildingRepository.FindAll(x => role_buildingIds.Contains(x.Id));
                }
                else return null;
                ctvm.Buildings =
                    (from b in userBuildings
                     select
                         new Node
                         {
                             ParentId = b.LocationId,
                             MyId = b.Id,
                             Name = b.Name
                         }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Towns =
                    (from t in (from ub in userBuildings select ub.Location)
                     where !t.IsDeleted
                     select
                         new Node
                         {
                             ParentId = t.CountryId,
                             MyId = t.Id,
                             Name = t.Name
                         }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Countries =
                    (from c in (from cbo in userBuildings select cbo.Location.Country).OrderBy(cc => cc.Name)
                     where !c.IsDeleted
                     select
                         new Node
                         {
                             ParentId = 0,
                             MyId = c.Id,
                             Name = c.Name
                         }).Distinct(new NodeEqualityComparer()).ToList();

                List<int> userBuildingIds = (from ub in userBuildings select ub.Id).ToList();
                var userBuildingObjects = _buildingObjectRepository.FindAll(x => !x.IsDeleted && userBuildingIds.Contains(x.BuildingId)).ToList();

                ctvm.Floors =
                    (from f in userBuildingObjects
                     where f.TypeId == (int)BuildingObjectTypes.Floor
                     select
                         new Node
                         {
                             ParentId = f.BuildingId,
                             MyId = f.Id,
                             Name = f.Description,
                             Comment = f.Comment
                         }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Objects =
                    (from o in userBuildingObjects
                     where o.ParentObjectId.HasValue && (o.TypeId == (int)BuildingObjectTypes.Door || o.TypeId == (int)BuildingObjectTypes.Lift || o.TypeId == (int)BuildingObjectTypes.Room)
                     orderby o.TypeId descending
                     select
                         new Node
                         {
                             ParentId = o.ParentObjectId.Value,
                             MyId = o.Id,
                             Name = o.ObjectNr.HasValue ? "#" + o.ObjectNr + " " + o.Description : o.Description,
                             Comment = o.Comment,
                             IsDefaultTimeZone = id.HasValue ? _userPermissionGroupService.IsDefaultUserTimeZone(o.Id, id.Value) : true,
                             IsRoom = o.TypeId == (int)BuildingObjectTypes.Room ? 1 : 0,
                             StatusIcon = o.StatusIconId.HasValue ? String.Format("../../img/status/{0}.ico", o.StatusIconId) : String.Empty
                         }).Distinct(new NodeEqualityComparer()).ToList();

                if (id.HasValue)
                {

                    ctvm.ActiveObjectIds = _userPermissionGroupService.GetUserBuildingObjectIds(id.Value);
                    foreach (var obn in ctvm.Objects)
                    {
                        if (obn.IsRoom == 1)
                        {
                            int idd = id.GetValueOrDefault();
                            UserPermissionGroupTimeZone pgtz = _userPermissionGroupTimeZoneRepository.FindByPGId(idd).Where(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();
                            obn.IsDisarming = pgtz == null ? true : pgtz.IsDisarming;
                            obn.IsDefaultDisarming = pgtz == null ? true : pgtz.IsDefaultDisarming;
                            obn.IsArming = pgtz == null ? true : pgtz.IsArming;
                            obn.IsDefaultArming = pgtz == null ? true : pgtz.IsDefaultArming;
                        }
                    }
                }
                else
                {
                    foreach (var obn in ctvm.Objects)
                    {
                        if (obn.IsRoom == 1)
                        {
                            obn.IsArming = obn.IsDisarming = obn.IsDefaultArming = obn.IsDefaultDisarming = true;
                        }
                    }
                }
                ctvm.Towns = ctvm.Towns.Where(town => ctvm.Buildings.Any(b => b.ParentId == town.MyId));
                ctvm.Countries = ctvm.Countries.Where(country => ctvm.Towns.Any(town => town.ParentId == country.MyId));
            }
            return PartialView("Tree", ctvm);
        }
Beispiel #2
0
        public ActionResult GetUserPermissionTree(int userId, int?id)
        {
            var ctvm             = CreateViewModel <PermissionTreeViewModel>();
            var role_buildingIds = GetUserBuildings(_buildingRepository, _userRepository, CurrentUser.Get().Id);

            if (id.HasValue)
            {
                var upg     = _userPermissionGroupRepository.FindById(id.Value);
                var cur_upg = _userPermissionGroupRepository.FindById(id.Value);

                /*
                 * if (CurrentUser.Get().IsCompanyManager)
                 * {
                 *  if (upg.UserId == userId)
                 *  {
                 *      upg = _userPermissionGroupRepository.FindById(upg.ParentUserPermissionGroupId.Value);
                 *      if( upg.UserId == CurrentUser.Get().Id)
                 *      {
                 *          ctvm.IsOriginal = false;
                 *          ctvm.IsCurrentUserAssignedGroup = upg.PermissionIsActive;
                 *      }
                 *  }
                 *  else
                 *  {*/
                ctvm.IsOriginal = false;
                ctvm.IsCurrentUserAssignedGroup = upg.PermissionIsActive;
                ctvm.IsCurrentUserAssignedGroup = false;

                /*
                 *  }
                 * }
                 */
                List <int> buildingObjectsIds = new List <int>();
                buildingObjectsIds = _userRepository.IsCompanyManager(userId) ? (from cbo in upg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted /*&& x.Active*/) select cbo.BuildingObjectId).ToList() : (from cbo in upg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted) select cbo.BuildingObjectId).ToList();
                var buildingObjects = _buildingObjectRepository.FindAll(x => !x.IsDeleted && /*role_buildingIds.Contains(x.BuildingId) &&*/ buildingObjectsIds.Contains(x.Id));
                if (CurrentUser.Get().IsBuildingAdmin || CurrentUser.Get().IsSuperAdmin || CurrentUser.Get().IsCompanyManager)
                {
                    var activePg            = _userPermissionGroupRepository.FindByUserId(userId).Where(x => x.PermissionIsActive && !x.IsDeleted).FirstOrDefault();
                    var userActiveBuildings = (from cbo in activePg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted && x.Active) select cbo.BuildingObjectId).ToList();
                    buildingObjects =
                        _buildingObjectRepository.FindAll(x => !x.IsDeleted && role_buildingIds.Contains(x.BuildingId) || userActiveBuildings.Contains(x.Id));
                }

                if (CurrentUser.Get().IsCompanyManager)
                {
                    var curr_upg_buildingIds =
                        (from cbo in cur_upg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted && x.Active)
                         select cbo.BuildingObjectId).ToList();

                    var activeUpg =
                        _userPermissionGroupRepository.FindByUserId(CurrentUser.Get().Id).Where(
                            x => x.PermissionIsActive && !x.IsDeleted).
                        FirstOrDefault();

                    var activeUpgBuildingIds = activeUpg != null
                                                   ? (from cbo in
                                                      activeUpg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted && x.Active)
                                                      select cbo.BuildingObjectId).ToList()
                                                   : null;

                    if (activeUpgBuildingIds != null)
                    {
                        foreach (var activeUpgBuildingId in activeUpgBuildingIds)
                        {
                            if (!curr_upg_buildingIds.Contains(activeUpgBuildingId))
                            {
                                curr_upg_buildingIds.Add(activeUpgBuildingId);
                            }
                        }
                    }
                    var activePg            = _userPermissionGroupRepository.FindByUserId(userId).Where(x => x.PermissionIsActive && !x.IsDeleted).FirstOrDefault();
                    var userActiveBuildings = (from cbo in activePg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted && x.Active) select cbo.BuildingObjectId).ToList();
                    buildingObjects = _buildingObjectRepository.FindAll(x => curr_upg_buildingIds.Contains(x.Id) && role_buildingIds.Contains(x.BuildingId) || userActiveBuildings.Contains(x.Id));
                }

                List <int> floorsId            = (from cbo in buildingObjects.Where(x => x.ParentObjectId.HasValue) select cbo.ParentObjectId.Value).ToList();
                var        companyFloorObjects = _buildingObjectRepository.FindAll(x => !x.IsDeleted && floorsId.Contains(x.Id));

                // start to make ctvm and html
                ctvm.Buildings =
                    (from b in
                     (from cbo in buildingObjects select cbo.Building)
                     where !b.IsDeleted
                     select
                     new Node
                {
                    ParentId = b.LocationId,
                    MyId = b.Id,
                    Name = b.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Towns =
                    (from t in
                     (from cbo in buildingObjects select cbo.Building.Location)
                     where !t.IsDeleted
                     select
                     new Node
                {
                    ParentId = t.CountryId,
                    MyId = t.Id,
                    Name = t.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Countries =
                    (from c in
                     (from cbo in buildingObjects select cbo.Building.Location.Country).OrderBy(cc => cc.Name)
                     where !c.IsDeleted
                     select
                     new Node
                {
                    ParentId = 0,
                    MyId = c.Id,
                    Name = c.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Floors =
                    (from f in companyFloorObjects
                     where f.TypeId == (int)BuildingObjectTypes.Floor
                     select
                     new Node
                {
                    ParentId = f.BuildingId,
                    MyId = f.Id,
                    Name = f.Description,
                    Comment = f.Comment
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Objects =
                    (from o in buildingObjects
                     where o.ParentObjectId.HasValue && (o.TypeId == (int)BuildingObjectTypes.Door || o.TypeId == (int)BuildingObjectTypes.Lift || o.TypeId == (int)BuildingObjectTypes.Room)
                     orderby o.TypeId descending
                     select
                     new Node
                {
                    ParentId = o.ParentObjectId.Value,
                    MyId = o.Id,
                    Name = o.ObjectNr.HasValue ? "#" + o.ObjectNr + " " + o.Description : o.Description,
                    Comment = o.Comment,
                    IsDefaultTimeZone = _userPermissionGroupService.IsDefaultUserTimeZone(o.Id, id.Value),
                    IsRoom = o.TypeId == (int)BuildingObjectTypes.Room ? 1 : 0,
                    StatusIcon = o.StatusIconId.HasValue ? String.Format("../../img/status/{0}.ico", o.StatusIconId) : String.Empty
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.ActiveObjectIds = _userPermissionGroupService.GetUserBuildingObjectIds(id.Value);
                foreach (var obn in ctvm.Objects)
                {
                    if (obn.IsRoom == 1)
                    {
                        //UserPermissionGroupTimeZone upgtz = _userPermissionGroupTimeZoneRepository.FindAll(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();
                        UserPermissionGroupTimeZone upgtz = _userPermissionGroupTimeZoneRepository.FindByPGId(id.Value).Where(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();
                        obn.IsArming           = upgtz == null ? true : upgtz.IsArming;
                        obn.IsDefaultArming    = upgtz == null ? true : upgtz.IsDefaultArming;
                        obn.IsDisarming        = upgtz == null ? true : upgtz.IsDisarming;
                        obn.IsDefaultDisarming = upgtz == null ? true : upgtz.IsDefaultDisarming;
                    }
                }

                ctvm.Towns     = ctvm.Towns.Where(town => ctvm.Buildings.Any(b => b.ParentId == town.MyId));
                ctvm.Countries = ctvm.Countries.Where(country => ctvm.Towns.Any(town => town.ParentId == country.MyId));
            }
            // return view
            return(PartialView("PermTree", ctvm));
            //return PartialView("Tree", ctvm);
        }