public override void Handle(FlowEvent @event, FlowContext context)
        {
            switch (@event)
            {
            case FlowEvent.UnableToLocateUser:
                var organizationByCvrResult = _organizationRepository.GetByCvr(_stsBrugerInfo.MunicipalityCvr);
                if (organizationByCvrResult.HasValue)
                {
                    var organization = organizationByCvrResult.Value;
                    var user         = CreateAutoProvisonedUser();
                    _organizationRoleService.MakeUser(user, organization);

                    context.TransitionTo(_ssoStateFactory.CreateUserLoggedIn(user),
                                         _ => _.HandleUserAutoProvisioned());
                }
                else
                {
                    context.TransitionTo(_ssoStateFactory.CreateErrorState(),
                                         _ => _.HandleUnableToLocateUser());
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(@event), @event, null);
            }
        }
Exemplo n.º 2
0
 public override void Handle(FlowEvent @event, FlowContext context)
 {
     if (@event.Equals(FlowEvent.UserHasNoRoleInOrganization))
     {
         _organizationRoleService.MakeUser(_user, _ssoOrganization);
         context.TransitionTo(_ssoStateFactory.CreateUserLoggedIn(_user), _ => _.HandleRoleAssigned());
     }
 }
Exemplo n.º 3
0
        public Result <Organization, OperationFailure> CreateNewOrganization(Organization newOrg)
        {
            if (newOrg == null)
            {
                throw new ArgumentNullException(nameof(newOrg));
            }
            var user = _userRepository.GetByKey(_userContext.UserId);

            if (user == null)
            {
                return(OperationFailure.Forbidden);
            }

            //Setup defaults
            newOrg.Uuid   = newOrg.Uuid == Guid.Empty ? Guid.NewGuid() : newOrg.Uuid;
            newOrg.Config = Config.Default(user);
            newOrg.OrgUnits.Add(new OrganizationUnit
            {
                Name = newOrg.Name,
            });

            if (newOrg.IsCvrInvalid())
            {
                _logger.Error("Invalid cvr {cvr} provided for org with name {name}", newOrg.Cvr, newOrg.Name);
                return(OperationFailure.BadInput);
            }

            if (!_userContext.OrganizationIds.Any(id => _authorizationContext.AllowCreate <Organization>(id)))
            {
                return(OperationFailure.Forbidden);
            }

            if (newOrg.TypeId > 0)
            {
                var organizationType = (OrganizationTypeKeys)newOrg.TypeId;
                var allowOrganizationTypeCreation = _userContext.OrganizationIds.Any(id => _authorizationContext.HasPermission(new DefineOrganizationTypePermission(organizationType, id)));
                if (!allowOrganizationTypeCreation)
                {
                    return(OperationFailure.Forbidden);
                }
            }
            else
            {
                //Invalid org key
                return(OperationFailure.BadInput);
            }

            using (var transaction = _transactionManager.Begin(IsolationLevel.Serializable))
            {
                newOrg = _orgRepository.Insert(newOrg);
                _orgRepository.Save();

                if (newOrg.TypeId == (int)OrganizationTypeKeys.Interessefællesskab)
                {
                    _organizationRoleService.MakeLocalAdmin(user, newOrg);
                    _organizationRoleService.MakeUser(user, newOrg);
                }

                transaction.Commit();
                return(newOrg);
            }
        }