public async Task <InvokeResult> CreateAuthorizedNewAsync([FromBody] RegisterUser newUser)
        {
            var result = await _appUserManager.CreateUserAsync(newUser, false, false);

            if (!result.Successful)
            {
                return(result.ToInvokeResult());
            }
            var setAuthResult = await _appUserManager.SetApprovedAsync(result.Result.User.Id, OrgEntityHeader, UserEntityHeader);

            if (!setAuthResult.Successful)
            {
                return(result.ToInvokeResult());
            }
            return(await _orgManager.AddUserToOrgAsync(OrgEntityHeader.Id, result.Result.User.Id, OrgEntityHeader, UserEntityHeader));
        }
        public async Task <InvokeResult <AppUser> > AddDeviceUser([FromBody] DeviceUserRegistrationRequest newuser)
        {
            String userId = Guid.NewGuid().ToId();

            newuser.Device.OwnerUser = EntityHeader.Create(userId, newuser.Email);

            var repo = await GetDeviceRepositoryWithSecretsAsync();

            var addDeviceResult = await _deviceManager.AddDeviceAsync(repo, newuser.Device, OrgEntityHeader, UserEntityHeader);

            if (!addDeviceResult.Successful)
            {
                return(InvokeResult <AppUser> .FromInvokeResult(addDeviceResult));
            }

            var appUser = new AppUser()
            {
                Id                   = userId,
                FirstName            = newuser.FirstName,
                LastName             = newuser.LastName,
                CurrentOrganization  = OrgEntityHeader,
                Email                = $"{repo.Id}-{newuser.Email}",
                PhoneNumber          = newuser.PhoneNumber,
                UserName             = $"{repo.Id}-{newuser.Email}",
                EmailConfirmed       = true,
                PhoneNumberConfirmed = true,
                IsAppBuilder         = false,
                IsOrgAdmin           = false,
                IsUserDevice         = true,
                PrimaryDevice        = EntityHeader.Create(newuser.Device.Id, newuser.Device.DeviceId),
                DeviceConfiguration  = EntityHeader.Create(newuser.Device.DeviceConfiguration.Id, newuser.Device.DeviceConfiguration.Text),
                DeviceRepo           = EntityHeader.Create(newuser.Device.DeviceRepository.Id, newuser.Device.DeviceRepository.Text),
                ProfileImageUrl      = new ImageDetails()
                {
                    Width    = 128,
                    Height   = 128,
                    ImageUrl = "https://bytemaster.blob.core.windows.net/userprofileimages/watermark.png",
                    Id       = "b78ca749a1e64ce59df4aa100050dcc7"
                }
            };


            SetAuditProperties(appUser);
            SetOwnedProperties(appUser);

            Console.WriteLine("Device Created  - " + newuser.Device.DeviceId);

            try
            {
                var result = await _userManager.CreateAsync(appUser, newuser.Password);

                if (result.Succeeded)
                {
                    var addToOrgResult = await _orgManager.AddUserToOrgAsync(OrgEntityHeader.Id, appUser.Id, OrgEntityHeader, UserEntityHeader);

                    if (addToOrgResult.Successful)
                    {
                        return(InvokeResult <AppUser> .Create(appUser));
                    }
                    else
                    {
                        await _userManager.DeleteAsync(appUser);

                        return(InvokeResult <AppUser> .FromInvokeResult(addToOrgResult));
                    }
                }
                else
                {
                    Console.WriteLine("Error creating user - removing device - " + newuser.Device.DeviceId);
                    var device = await _deviceManager.GetDeviceByDeviceIdAsync(repo, newuser.Device.DeviceId, OrgEntityHeader, UserEntityHeader);

                    await _deviceManager.DeleteDeviceAsync(repo, device.Id, OrgEntityHeader, UserEntityHeader);

                    return(InvokeResult <AppUser> .FromError(result.Errors.First().Description));
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Exception - removing device - " + newuser.Device.DeviceId);
                var device = await _deviceManager.GetDeviceByDeviceIdAsync(repo, newuser.Device.DeviceId, OrgEntityHeader, UserEntityHeader);

                await _deviceManager.DeleteDeviceAsync(repo, device.Id, OrgEntityHeader, UserEntityHeader);

                throw;
            }
        }
示例#3
0
        public async Task <InvokeResult> AddClientAppAsync(ClientApp clientApp, EntityHeader org, EntityHeader user)
        {
            ValidationCheck(clientApp, Actions.Create);

            await AuthorizeAsync(clientApp, AuthorizeResult.AuthorizeActions.Create, user, org);

            var primaryAddResult = await _secureStorage.AddSecretAsync(org, clientApp.AppAuthKeyPrimary);

            if (!primaryAddResult.Successful)
            {
                return(primaryAddResult.ToInvokeResult());
            }

            var secondaryAddResult = await _secureStorage.AddSecretAsync(org, clientApp.AppAuthKeySecondary);

            if (!secondaryAddResult.Successful)
            {
                return(secondaryAddResult.ToInvokeResult());
            }

            clientApp.AppAuthKeyPrimarySecureId = primaryAddResult.Result;
            clientApp.AppAuthKeyPrimary         = null;

            clientApp.AppAuthKeySecondarySecureId = secondaryAddResult.Result;
            clientApp.AppAuthKeySecondary         = null;

            var clientAppUserId = Guid.NewGuid().ToId();

            clientApp.ClientAppUser = EntityHeader.Create(clientAppUserId, $"{clientApp.Key} Service Account");

            var fullOrg = await _orgManager.GetOrganizationAsync(org.Id, org, user);

            var clientAppEmail = $"{fullOrg.Namespace}.{clientApp.Key}@nodomain.cantlogin";

            var result = await _userManager.CreateAsync(new UserAdmin.Models.Users.AppUser()
            {
                CurrentOrganization = org,
                Email                = clientAppEmail,
                FirstName            = clientApp.Name,
                LastName             = "Service Account",
                Id                   = clientAppUserId,
                UserName             = clientAppEmail,
                OwnerOrganization    = org,
                IsAppBuilder         = true,
                IsOrgAdmin           = false,
                IsSystemAdmin        = false,
                IsRuntimeuser        = true,
                PhoneNumberConfirmed = true,
                EmailConfirmed       = true,
                CreationDate         = clientApp.CreationDate,
                LastUpdatedDate      = clientApp.CreationDate,
                LastUpdatedBy        = user,
                CreatedBy            = user,
                IsAccountDisabled    = false,
                Name                 = clientApp.ClientAppUser.Text,
                PhoneNumber          = "612 555-1212",
            }, $"NuvI0Tabc{Guid.NewGuid().ToId()}");


            if (!result.Successful)
            {
                return(result);
            }

            await _orgManager.AddUserToOrgAsync(org.Id, clientAppUserId, org, user);

            await _repo.AddClientAppAsync(clientApp);

            return(InvokeResult.Success);
        }
 public async Task <InvokeResult> AddAccountToOrgAsync(string orgid, string userid)
 {
     return(await _orgManager.AddUserToOrgAsync(orgid, userid, OrgEntityHeader, UserEntityHeader));
 }