Пример #1
0
        public void GetAll_Test()
        {
            authService.Add(new User());
            authService.Add(new User());
            authService.Add(new User());
            goodsService.Add(new Goods());
            shopOrderService.Add(new ShopOrder());
            shopOrderService.Add(new ShopOrder());
            typeOfGoodsService.Add(new TypeOfGoods());
            typeOfGoodsService.Add(new TypeOfGoods());
            typeOfGoodsService.Add(new TypeOfGoods());

            var statistic = new StatisticService(authService, goodsService, typeOfGoodsService, shopOrderService).Get();

            Assert.AreEqual(statistic.TotalCountOfGoods, 1);
            Assert.AreEqual(statistic.TotalCountOfGoodsTypes, 3);
            Assert.AreEqual(statistic.TotalCountOfUsers, 3);
            Assert.AreEqual(statistic.TotalCountOfOrders, 2);
        }
Пример #2
0
        /// <summary>
        /// Handles the Click event of the lbAddRole control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbAddRole_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var authService = new AuthService(rockContext);

                foreach (ListItem li in cblRoleActionList.Items)
                {
                    if (li.Selected)
                    {
                        Rock.Model.SpecialRole specialRole = Rock.Model.SpecialRole.None;
                        int?groupId = ddlRoles.SelectedValue.AsIntegerOrNull();

                        switch (groupId)
                        {
                        case -1: specialRole = Rock.Model.SpecialRole.AllUsers;
                            break;

                        case -2: specialRole = Rock.Model.SpecialRole.AllAuthenticatedUsers;
                            break;

                        case -3: specialRole = Rock.Model.SpecialRole.AllUnAuthenticatedUsers;
                            break;

                        default: specialRole = Rock.Model.SpecialRole.None;
                            break;
                        }

                        if (groupId < 0)
                        {
                            groupId = null;
                        }

                        var existingAuths = authService.GetAuths(iSecured.TypeId, iSecured.Id, li.Text).ToList();
                        if (!existingAuths.Any(a => a.SpecialRole == specialRole && a.GroupId.Equals(groupId)))
                        {
                            int order = existingAuths.Count > 0 ? existingAuths.Last().Order + 1 : 0;

                            Rock.Model.Auth auth = new Rock.Model.Auth();
                            auth.EntityTypeId = iSecured.TypeId;
                            auth.EntityId     = iSecured.Id;
                            auth.Action       = li.Text;
                            auth.AllowOrDeny  = "A";
                            auth.SpecialRole  = specialRole;
                            auth.GroupId      = groupId;
                            auth.Order        = order;
                            authService.Add(auth);

                            rockContext.SaveChanges();

                            Authorization.ReloadAction(iSecured.TypeId, iSecured.Id, li.Text);
                        }
                    }
                }
            }

            pnlAddRole.Visible = false;
            phList.Visible     = true;

            BindGrid();
        }
Пример #3
0
        /// <summary>
        /// Copies the authorizations from one <see cref="ISecured"/> object to another
        /// </summary>
        /// <param name="sourceEntity">The source entity.</param>
        /// <param name="targetEntity">The target entity.</param>
        /// <param name="personId">The person id.</param>
        public static void CopyAuthorization(ISecured sourceEntity, ISecured targetEntity, int?personId)
        {
            using (new Rock.Data.UnitOfWorkScope())
            {
                // If there's no Authorizations object, create it
                if (Authorizations == null)
                {
                    Load();
                }

                AuthService authService = new AuthService();

                // Delete the current authorizations for the target entity
                foreach (Auth auth in authService.GetByEntityTypeAndEntityId(targetEntity.AuthEntity, targetEntity.Id))
                {
                    authService.Delete(auth, personId);
                }

                Dictionary <string, List <AuthRule> > newActions = new Dictionary <string, List <AuthRule> >();

                int order = 0;
                if (Authorizations.ContainsKey(sourceEntity.AuthEntity) && Authorizations[sourceEntity.AuthEntity].ContainsKey(sourceEntity.Id))
                {
                    foreach (KeyValuePair <string, List <AuthRule> > action in Authorizations[sourceEntity.AuthEntity][sourceEntity.Id])
                    {
                        if (targetEntity.SupportedActions.Contains(action.Key))
                        {
                            newActions.Add(action.Key, new List <AuthRule>());

                            foreach (AuthRule rule in action.Value)
                            {
                                Auth auth = new Auth();
                                auth.EntityType  = targetEntity.AuthEntity;
                                auth.EntityId    = targetEntity.Id;
                                auth.Order       = order;
                                auth.Action      = action.Key;
                                auth.AllowOrDeny = rule.AllowOrDeny;
                                auth.SpecialRole = rule.SpecialRole;
                                auth.PersonId    = rule.PersonId;
                                auth.GroupId     = rule.GroupId;

                                authService.Add(auth, personId);
                                authService.Save(auth, personId);

                                newActions[action.Key].Add(new AuthRule(rule.Id, rule.AllowOrDeny, rule.SpecialRole, rule.PersonId, rule.GroupId, rule.Order));

                                order++;
                            }
                        }
                    }
                }

                if (!Authorizations.ContainsKey(targetEntity.AuthEntity))
                {
                    Authorizations.Add(targetEntity.AuthEntity, new Dictionary <int, Dictionary <string, List <AuthRule> > >());
                }

                Dictionary <int, Dictionary <string, List <AuthRule> > > entityType = Authorizations[targetEntity.AuthEntity];

                if (!entityType.ContainsKey(targetEntity.Id))
                {
                    entityType.Add(targetEntity.Id, new Dictionary <string, List <AuthRule> >());
                }

                entityType[targetEntity.Id] = newActions;
            }
        }
Пример #4
0
        public void Add_Test()
        {
            service.Add(new User()
            {
                Id = 1, FullName = "test 1"
            });
            service.Add(new User()
            {
                Id = 2, FullName = "test 2"
            });
            service.Add(new User()
            {
                Id = 3, FullName = "test 3"
            });

            var check = service.Get(2);

            Assert.NotNull(check);
            Assert.AreEqual(check.FullName, "test 2");
        }
Пример #5
0
        /// <summary>
        /// Copies the authorizations from one <see cref="ISecured" /> object to another
        /// </summary>
        /// <param name="sourceEntity">The source entity.</param>
        /// <param name="targetEntity">The target entity.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <remarks>
        /// This method will save any previous changes made to the context
        /// </remarks>
        public static void CopyAuthorization(ISecured sourceEntity, ISecured targetEntity, RockContext rockContext)
        {
            // If there's no Authorizations object, create it
            if (Authorizations == null)
            {
                Load();
            }

            var sourceEntityTypeId = sourceEntity.TypeId;
            var targetEntityTypeId = targetEntity.TypeId;

            AuthService authService = new AuthService(rockContext);

            // Delete the current authorizations for the target entity
            foreach (Auth auth in authService.Get(targetEntityTypeId, targetEntity.Id))
            {
                authService.Delete(auth);
            }

            Dictionary <string, List <AuthRule> > newActions = new Dictionary <string, List <AuthRule> >();

            int order = 0;

            if (Authorizations.ContainsKey(sourceEntityTypeId) && Authorizations[sourceEntityTypeId].ContainsKey(sourceEntity.Id))
            {
                foreach (KeyValuePair <string, List <AuthRule> > action in Authorizations[sourceEntityTypeId][sourceEntity.Id])
                {
                    if (targetEntity.SupportedActions.ContainsKey(action.Key))
                    {
                        newActions.Add(action.Key, new List <AuthRule>());

                        foreach (AuthRule rule in action.Value)
                        {
                            Auth auth = new Auth();
                            auth.EntityTypeId  = targetEntityTypeId;
                            auth.EntityId      = targetEntity.Id;
                            auth.Order         = order;
                            auth.Action        = action.Key;
                            auth.AllowOrDeny   = rule.AllowOrDeny;
                            auth.SpecialRole   = rule.SpecialRole;
                            auth.PersonAliasId = rule.PersonAliasId;
                            auth.GroupId       = rule.GroupId;

                            authService.Add(auth);

                            newActions[action.Key].Add(new AuthRule(rule.Id, targetEntity.Id, rule.AllowOrDeny, rule.SpecialRole, rule.PersonId, rule.PersonAliasId, rule.GroupId, rule.Order));

                            order++;
                        }
                    }
                }
            }

            rockContext.SaveChanges();

            if (!Authorizations.ContainsKey(targetEntityTypeId))
            {
                Authorizations.Add(targetEntityTypeId, new Dictionary <int, Dictionary <string, List <AuthRule> > >());
            }

            Dictionary <int, Dictionary <string, List <AuthRule> > > entityType = Authorizations[targetEntityTypeId];

            if (!entityType.ContainsKey(targetEntity.Id))
            {
                entityType.Add(targetEntity.Id, new Dictionary <string, List <AuthRule> >());
            }

            entityType[targetEntity.Id] = newActions;
        }
Пример #6
0
        private static void MyAllowPerson(ISecured entity, string action, Person person, RockContext rockContext)
        {
            if (person != null)
            {
                rockContext = rockContext ?? new RockContext();

                var personAlias = new PersonAliasService(rockContext).GetPrimaryAlias(person.Id);
                if (personAlias != null)
                {
                    // If there's no Authorizations object, create it
                    if (Authorizations == null)
                    {
                        Load();
                    }

                    var authService = new AuthService(rockContext);

                    // If there are not entries in the Authorizations object for this entity type and entity instance, create
                    // the dictionary entries
                    if (!Authorizations.Keys.Contains(entity.TypeId))
                    {
                        Authorizations.Add(entity.TypeId, new Dictionary <int, Dictionary <string, List <AuthRule> > >());
                    }

                    if (!Authorizations[entity.TypeId].Keys.Contains(entity.Id))
                    {
                        Authorizations[entity.TypeId].Add(entity.Id, new Dictionary <string, List <AuthRule> >());
                    }

                    List <AuthRule> rules = null;
                    if (Authorizations[entity.TypeId][entity.Id].Keys.Contains(action))
                    {
                        rules = Authorizations[entity.TypeId][entity.Id][action];
                    }
                    else
                    {
                        rules = new List <AuthRule>();
                        Authorizations[entity.TypeId][entity.Id].Add(action, rules);
                    }

                    int order = 0;

                    Auth auth = new Auth();
                    auth.EntityTypeId  = entity.TypeId;
                    auth.EntityId      = entity.Id;
                    auth.Order         = order++;
                    auth.Action        = action;
                    auth.AllowOrDeny   = "A";
                    auth.SpecialRole   = SpecialRole.None;
                    auth.PersonAlias   = personAlias;
                    auth.PersonAliasId = personAlias.Id;
                    authService.Add(auth);

                    foreach (var rule in rules)
                    {
                        var existingAuth = authService.Get(rule.Id);
                        existingAuth.Order = order++;
                    }

                    rockContext.SaveChanges();

                    rules.Insert(0, new AuthRule(auth));
                }
            }
        }
Пример #7
0
        private static void MyMakePrivate(ISecured entity, string action, Person person, RockContext rockContext)
        {
            if (!IsPrivate(entity, action, person))
            {
                if (person != null)
                {
                    var personAlias = new PersonAliasService(rockContext).GetPrimaryAlias(person.Id);
                    if (personAlias != null)
                    {
                        // If there's no Authorizations object, create it
                        if (Authorizations == null)
                        {
                            Load();
                        }

                        var authService = new AuthService(rockContext);

                        // If there are not entries in the Authorizations object for this entity type and entity instance, create
                        // the dictionary entries
                        if (!Authorizations.Keys.Contains(entity.TypeId))
                        {
                            Authorizations.Add(entity.TypeId, new Dictionary <int, Dictionary <string, List <AuthRule> > >());
                        }

                        if (!Authorizations[entity.TypeId].Keys.Contains(entity.Id))
                        {
                            Authorizations[entity.TypeId].Add(entity.Id, new Dictionary <string, List <AuthRule> >());
                        }

                        if (!Authorizations[entity.TypeId][entity.Id].Keys.Contains(action))
                        {
                            Authorizations[entity.TypeId][entity.Id].Add(action, new List <AuthRule>());
                        }
                        else
                        {
                            // If existing rules exist, delete them.
                            foreach (AuthRule authRule in Authorizations[entity.TypeId][entity.Id][action])
                            {
                                var oldAuth = authService.Get(authRule.Id);
                                authService.Delete(oldAuth);
                            }
                        }

                        var rules = new List <AuthRule>();

                        Auth auth1 = new Auth();
                        auth1.EntityTypeId  = entity.TypeId;
                        auth1.EntityId      = entity.Id;
                        auth1.Order         = 0;
                        auth1.Action        = action;
                        auth1.AllowOrDeny   = "A";
                        auth1.SpecialRole   = SpecialRole.None;
                        auth1.PersonAlias   = personAlias;
                        auth1.PersonAliasId = personAlias.Id;
                        authService.Add(auth1);

                        Auth auth2 = new Auth();
                        auth2.EntityTypeId = entity.TypeId;
                        auth2.EntityId     = entity.Id;
                        auth2.Order        = 1;
                        auth2.Action       = action;
                        auth2.AllowOrDeny  = "D";
                        auth2.SpecialRole  = SpecialRole.AllUsers;
                        authService.Add(auth2);

                        rockContext.SaveChanges();

                        rules.Add(new AuthRule(auth1));
                        rules.Add(new AuthRule(auth2));

                        Authorizations[entity.TypeId][entity.Id][action] = rules;
                    }
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Updates authorization rules for the entity so that the current person is allowed to perform the specified action.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="action">The action.</param>
        /// <param name="person">The person.</param>
        /// <param name="personId">The person identifier.</param>
        public static void AllowPerson(ISecured entity, string action, Person person, int?personId)
        {
            if (person != null)
            {
                // If there's no Authorizations object, create it
                if (Authorizations == null)
                {
                    Load();
                }

                var authService = new AuthService();

                // If there are not entries in the Authorizations object for this entity type and entity instance, create
                // the dictionary entries
                if (!Authorizations.Keys.Contains(entity.TypeId))
                {
                    Authorizations.Add(entity.TypeId, new Dictionary <int, Dictionary <string, List <AuthRule> > >());
                }

                if (!Authorizations[entity.TypeId].Keys.Contains(entity.Id))
                {
                    Authorizations[entity.TypeId].Add(entity.Id, new Dictionary <string, List <AuthRule> >());
                }

                List <AuthRule> rules = null;
                if (Authorizations[entity.TypeId][entity.Id].Keys.Contains(action))
                {
                    rules = Authorizations[entity.TypeId][entity.Id][action];
                }
                else
                {
                    rules = new List <AuthRule>();
                    Authorizations[entity.TypeId][entity.Id].Add(action, rules);
                }

                int order = 0;

                Rock.Data.RockTransactionScope.WrapTransaction(() =>
                {
                    Auth auth         = new Auth();
                    auth.EntityTypeId = entity.TypeId;
                    auth.EntityId     = entity.Id;
                    auth.Order        = order++;
                    auth.Action       = action;
                    auth.AllowOrDeny  = "A";
                    auth.SpecialRole  = SpecialRole.None;
                    auth.PersonId     = person.Id;
                    authService.Add(auth, personId);
                    authService.Save(auth, personId);

                    foreach (var rule in rules)
                    {
                        var existingAuth   = authService.Get(rule.Id);
                        existingAuth.Order = order++;
                        authService.Save(existingAuth, personId);
                    }

                    rules.Insert(0, new AuthRule(auth));
                });
            }
        }
Пример #9
0
        /// <summary>
        /// Handles the Click event of the btnCopy control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbCopyButton_Click(object sender, EventArgs e)
        {
            var rockContext      = new RockContext();
            var groupService     = new GroupService(rockContext);
            var authService      = new AuthService(rockContext);
            var attributeService = new AttributeService(rockContext);

            int groupId = hfGroupId.ValueAsInt();
            var group   = groupService.Queryable("GroupType")
                          .Where(g => g.Id == groupId)
                          .FirstOrDefault();

            if (group != null)
            {
                group.LoadAttributes(rockContext);

                // clone the group
                var newGroup = group.Clone(false);
                newGroup.CreatedByPersonAlias    = null;
                newGroup.CreatedByPersonAliasId  = null;
                newGroup.CreatedDateTime         = RockDateTime.Now;
                newGroup.ModifiedByPersonAlias   = null;
                newGroup.ModifiedByPersonAliasId = null;
                newGroup.ModifiedDateTime        = RockDateTime.Now;
                newGroup.Id       = 0;
                newGroup.Guid     = Guid.NewGuid();
                newGroup.IsSystem = false;
                newGroup.Name     = group.Name + " - Copy";

                if (GetAttributeValue("CopyLocation").AsBoolean(true))
                {
                    foreach (GroupLocation location in group.GroupLocations)
                    {
                        newGroup.GroupLocations.Add(location);
                    }
                }

                var auths = authService.GetByGroup(group.Id);
                rockContext.WrapTransaction(() =>
                {
                    groupService.Add(newGroup);
                    rockContext.SaveChanges();

                    newGroup.LoadAttributes(rockContext);
                    if (group.Attributes != null && group.Attributes.Any())
                    {
                        foreach (var attributeKey in group.Attributes.Select(a => a.Key))
                        {
                            string value = group.GetAttributeValue(attributeKey);
                            newGroup.SetAttributeValue(attributeKey, value);
                        }
                    }

                    newGroup.SaveAttributeValues(rockContext);

                    /* Take care of Group Member Attributes */
                    var entityTypeId       = EntityTypeCache.Get(typeof(GroupMember)).Id;
                    string qualifierColumn = "GroupId";
                    string qualifierValue  = group.Id.ToString();

                    // Get the existing attributes for this entity type and qualifier value
                    var attributes = attributeService.Get(entityTypeId, qualifierColumn, qualifierValue);

                    foreach (var attribute in attributes)
                    {
                        var newAttribute      = attribute.Clone(false);
                        newAttribute.Id       = 0;
                        newAttribute.Guid     = Guid.NewGuid();
                        newAttribute.IsSystem = false;
                        newAttribute.EntityTypeQualifierValue = newGroup.Id.ToString();

                        foreach (var qualifier in attribute.AttributeQualifiers)
                        {
                            var newQualifier      = qualifier.Clone(false);
                            newQualifier.Id       = 0;
                            newQualifier.Guid     = Guid.NewGuid();
                            newQualifier.IsSystem = false;

                            newAttribute.AttributeQualifiers.Add(qualifier);
                        }

                        attributeService.Add(newAttribute);
                    }

                    rockContext.SaveChanges();

                    var person = CurrentPerson;
                    GroupMember currentGroupMember = null;
                    if (person != null)
                    {
                        currentGroupMember = group.Members.Where(gm => gm.PersonId == person.Id).FirstOrDefault();
                    }
                    if (currentGroupMember != null)
                    {
                        var newGroupMember      = currentGroupMember.Clone(false);
                        newGroupMember.Id       = 0;
                        newGroupMember.Guid     = Guid.NewGuid();
                        newGroupMember.IsSystem = false;
                        newGroup.Members.Add(newGroupMember);
                    }

                    rockContext.SaveChanges();

                    foreach (var auth in auths)
                    {
                        var newAuth     = auth.Clone(false);
                        newAuth.Id      = 0;
                        newAuth.Guid    = Guid.NewGuid();
                        newAuth.GroupId = newGroup.Id;
                        newAuth.CreatedByPersonAlias    = null;
                        newAuth.CreatedByPersonAliasId  = null;
                        newAuth.CreatedDateTime         = RockDateTime.Now;
                        newAuth.ModifiedByPersonAlias   = null;
                        newAuth.ModifiedByPersonAliasId = null;
                        newAuth.ModifiedDateTime        = RockDateTime.Now;
                        authService.Add(newAuth);
                    }

                    rockContext.SaveChanges();
                    Rock.Security.Authorization.Clear();
                });

                NavigateToCurrentPage(new Dictionary <string, string> {
                    { "GroupId", newGroup.Id.ToString() }
                });
            }
        }