protected ActionResult ProcessSubmit(UserGroupEditorModel model, UserGroup entity)
        {
            Mandate.ParameterNotNull(model, "model");

            //bind it's data
            model.BindModel(this);

            //if there's model errors, return the view
            if (!ModelState.IsValid)
            {
                AddValidationErrorsNotification();
                return View("Edit", model);
            }

            //persist the data
            using (var uow = Hive.Create<ISecurityStore>())
            {

                if (entity == null)
                {
                    //map to new entity
                    entity =
                        BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map
                            <UserGroupEditorModel, UserGroup>(model);
                }
                else
                {
                    //map to existing entity
                    BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map(model, entity);
                }

                uow.Repositories.AddOrUpdate(entity);

                // Save permissions
                var metaDataumList = new List<RelationMetaDatum>();
                foreach (var permissionModel in model.Permissions)
                {
                    var permission = BackOfficeRequestContext.RegisteredComponents.Permissions.SingleOrDefault(x => x.Metadata.Id == permissionModel.PermissionId);
                    if (permission == null)
                        throw new NullReferenceException("Could not find permission with id " + permissionModel.PermissionId);

                    metaDataumList.Add(BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map<RelationMetaDatum>(permissionModel));
                }

                // Change permissions relation
                uow.Repositories.ChangeOrCreateRelationMetadata(entity.Id, FixedHiveIds.SystemRoot, FixedRelationTypes.PermissionRelationType, metaDataumList.ToArray());
                
                uow.Complete();

                Notifications.Add(new NotificationMessage(
                    "UserGroup.Save.Message".Localize(this),
                    "UserGroup.Save.Title".Localize(this),
                    NotificationType.Success));

                //add path for entity for SupportsPathGeneration (tree syncing) to work
                GeneratePathsForCurrentEntity(uow.Repositories.GetEntityPaths<TypedEntity>(entity.Id, FixedRelationTypes.DefaultRelationType));
                
                return RedirectToAction("Edit", new { id = entity.Id });
            }

        }
        /// <summary>
        /// Populates the permissions for the given model.
        /// </summary>
        /// <param name="model">The model.</param>
        private void PopulatePermissions(UserGroupEditorModel model)
        {
            // Get all permissions
            var permissions = BackOfficeRequestContext.RegisteredComponents.Permissions
                .Select(x => x.Metadata)
                .OrderByDescending(x => x.Type)
                .ThenBy(x => x.Name)
                .ToList();

            var permissionStatusModels = permissions.Select(x => BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map<PermissionStatusModel>(x)).ToList();
            
            //TODO: There is currently a bug with hive id when returned by relations where the ProviderGroupRoot is null, so we have to create a similar hive id to do comparisons with
            //var userGroupId = new HiveId((Uri)null, model.Id.ProviderId, model.Id.Value);
            var userGroupId = model.Id;

            foreach (var permissionStatusModel in permissionStatusModels)
            {
                // Set status
                var permissionInheritKey = "__permission___" + permissionStatusModel.PermissionId + "_inherit";
                var permissionStatusKey = "__permission___" + permissionStatusModel.PermissionId + "_status";
                permissionStatusModel.Status = !string.IsNullOrWhiteSpace(Request.Form[permissionInheritKey]) 
                    ? PermissionStatus.Inherit
                    : !string.IsNullOrWhiteSpace(Request.Form[permissionStatusKey])
                        ? (PermissionStatus)Enum.Parse(typeof(PermissionStatus), Request.Form[permissionStatusKey])
                        : BackOfficeRequestContext.Application.Security.GetExplicitPermission(permissionStatusModel.PermissionId, new[] { userGroupId }, FixedHiveIds.SystemRoot).Status;
            }

            model.Permissions = permissionStatusModels;
        }