예제 #1
0
        protected void AddGroupRole(GroupEntityModel group, ApplicationRoles role)
        {
            // Add Group to Role.
            var groupRole = new GroupRoleEntityModel
            {
                Group     = group,
                Role      = Context.Roles.Find(role.GetEnumGuid()),
                CreatedBy = User
            };

            Context.GroupRoles.Add(groupRole);
            Context.SaveChanges();
            Trace.WriteLine($"Added Role to Group -> {role} => {group.Name}");
        }
예제 #2
0
        public IGroupDto Save(IGroupDto group)
        {
            GroupEntityModel update;
            UpdateResultDto  result = new UpdateResultDto(false, "Unknown Error Occured.");

            // Check for New Group.
            if (group.GroupGuid == Guid.Empty)
            {
                // Verify Access for Add.
                if (!UserHasRoles(ApplicationRoles.GroupCreate))
                {
                    // No Ability to Add.
                    group.UpdateResult = new UpdateResultDto(false, "You do not have access to create a record.");
                    return(group);
                }

                // Get Entity.
                update = Mapper.Map <GroupEntityModel>(group);

                // Set Creation.
                update.CreatedByUserGuid = User.UserGuid;
                foreach (var role in update.Roles)
                {
                    role.CreatedByUserGuid = User.UserGuid;
                }

                // Create Default Membership.
                var userGroup = new UserGroupEntityModel()
                {
                    UserGuid          = User.UserGuid,
                    Group             = update,
                    CreatedByUserGuid = User.UserGuid
                };
                Db.Entry(userGroup).State = EntityState.Added;

                // Add Entity.
                Db.Entry(update).State = EntityState.Added;

                // Set Result.
                result = new UpdateResultDto(true, "Group Created Successfully.");
            }
            else
            {
                // Verify Access to Edit.
                if ((!AllowedGroups?.Any(g => g.GroupGuid == group.GroupGuid) ?? true) ||
                    !UserHasRoles(ApplicationRoles.GroupEdit))
                {
                    // No Ability to Edit.
                    group.UpdateResult = new UpdateResultDto(false, "You do not have access to modify this record.");
                    return(group);
                }

                // Load Group for Update.s
                update = AllowedGroups.FirstOrDefault(g => g.GroupGuid == group.GroupGuid);

                // Check Group.
                if (update != null)
                {
                    // Update Group.
                    update.Name              = group.Name;
                    update.Description       = group.Description;
                    update.UpdatedByUserGuid = User.UserGuid;
                    update.UpdatedDt         = DateTimeOffset.UtcNow;

                    // Parse Roles for Update/Delete.
                    foreach (var role in update.Roles.ToList())
                    {
                        // Update Existing Record.
                        if (group.Roles?.Any(r => r.Key == role.RoleGuid) ?? false)
                        {
                            role.UpdatedByUserGuid = User.UserGuid;
                            role.UpdatedDt         = DateTimeOffset.UtcNow;
                        }
                        else // Delete Existing Record.
                        {
                            Db.GroupRoles.Remove(role);
                        }
                    }

                    // Parse Roles for Add.
                    if (group.Roles != null)
                    {
                        var existing = update.Roles.Select(r => r.RoleGuid).ToList();
                        foreach (var role in group.Roles.Where(r => !existing.Contains(r.Key)))
                        {
                            // Add New Role.
                            var groupRole = new GroupRoleEntityModel()
                            {
                                Group             = update,
                                RoleGuid          = role.Key,
                                CreatedByUserGuid = User.UserGuid
                            };
                            update.Roles.Add(groupRole);
                        }
                    }

                    // Update Entry.
                    Db.Entry(update).State = EntityState.Modified;

                    // Set Result.
                    result = new UpdateResultDto(true, "Group Updated Successfully.");
                }
            }

            // Save.
            Db.SaveChanges();

            // Update User Roles.
            Db.Database.ExecuteSqlCommand("Access.usp_UserGroupRoleSync");

            // Load Group.
            group = GetById(update?.GroupGuid.ToString() ?? group.GroupGuid.ToString());

            // Set Update Result.
            group.UpdateResult = result;

            return(group);
        }