Ejemplo n.º 1
0
        public Organisation CreateOrganisation(CreateOrganisation dto)
        {
            var org = new Organisation()
            {
                Name              = dto.Name,
                TelNumber         = dto.TelNumber,
                AddressLine1      = dto.AddressLine1,
                AddressLine2      = dto.AddressLine2,
                Town              = dto.Town,
                County            = dto.County,
                Postcode          = dto.Postcode,
                DefaultLanguageId = dto.DefaultLanguageId,
                DefaultCalendarId = dto.DefaultCalendarId,
                IsActive          = true
            };

            var rootUser = new OrgUser()
            {
                Organisation = org,
                IsRootUser   = true,
                IsWebUser    = true,
                IsMobileUser = false,
                UserName     = dto.RootUserEmail,
                Email        = dto.RootUserEmail,
                FirstName    = dto.RootUserFirstName,
                Surname      = dto.RootUserSurname,
                TypeId       = OrgUserTypesRepository.Administrator.Id,
                AccountType  = AccountType.WebAccount
            };

            CurrentUOW.OrganisationRepository.InsertOrUpdate(org);

            using (var tran = CurrentUOW.Context.Database.BeginTransaction())
            {
                CurrentUOW.Save();

                var identityResult = CurrentUOW.UserManager.CreateSync(rootUser, dto.RootPassword);
                if (!identityResult.Succeeded)
                {
                    throw new Exception(identityResult.Errors.ToString(". "));
                }

                foreach (var role in OrgUserTypesRepository.Administrator.GetRoles())
                {
                    identityResult = CurrentUOW.UserManager.AddToRoleSync(rootUser.Id, role);
                    if (!identityResult.Succeeded)
                    {
                        throw new Exception(identityResult.Errors.ToString(". "));
                    }
                }

                org.RootUser = rootUser;
                CurrentUOW.Save();

                // if user manager created the application user successfully
                tran.Commit();
            }

            return(org);
        }
Ejemplo n.º 2
0
        // POST api/organisations
        public IHttpActionResult Post([FromBody] OrganisationDTO value)
        {
            var createOrganisation = new CreateOrganisation();

            createOrganisation.Name                = value.Name;
            createOrganisation.RootUserEmail       = value.RootUser.Email;
            createOrganisation.RootUserFirstName   = value.RootUser.FirstName;
            createOrganisation.RootUserSurname     = value.RootUser.Surname;
            createOrganisation.RootPassword        = value.RootUser.Password;
            createOrganisation.RootConfirmPassword = value.RootUser.ConfirmPassword;
            createOrganisation.AddressLine1        = value.AddressLine1;
            createOrganisation.AddressLine2        = value.AddressLine2;
            createOrganisation.County              = value.County;
            createOrganisation.Town                = value.Town;
            createOrganisation.Postcode            = value.Postcode;
            createOrganisation.TelNumber           = value.TelNumber;
            createOrganisation.DefaultCalendarId   = CalendarsRepository.Gregorian.Id;
            createOrganisation.DefaultLanguageId   = LanguagesRepository.English.Id;

            Organisations.CreateOrganisation(createOrganisation);

            try
            {
                UnitOfWork.Save();
                MemoryCacher.DeleteStartingWith(CACHE_KEY);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }