Esempio n. 1
0
        // todo: refactor this
        // todo: fix domain validation
        public async Task <AppUser> AddUserAsync(AppUser u, bool emailInvite = false, bool isCustomerAdmin = false, bool validateDomain = false)
        {
            if (validateDomain)
            {
                var emailPieces = u.Email.Split('@');
                var valid       = ValidateTenantAgainstDomain(emailPieces[1], u.TenantId);
                if (!valid)
                {
                    throw new SecurityException("User admin tenant does not match added user tenant");
                }
            }
            var user = await _repo.AddUserAsync(new AppUserEntity(u.TenantId, u.Email)
            {
                DisplayName     = u.DisplayName,
                AddedBy         = u.AddedBy,
                DateAdded       = u.DateAdded,
                NameIdentifier  = u.NameIdentifier,
                InviteRedeemUrl = u.InviteRedeemUrl,
                InvitedUserId   = u.InvitedUserId,
                Upn             = u.Upn
            });

            var inviteResult = await _graph.InviteUser(u.Email, emailInvite, u.DisplayName);

            user.Value.InviteRedeemUrl = inviteResult.InvitedUserInviteRedeemUrl;
            user.Value.InvitedUserId   = inviteResult.InvitedUserId;
            await _repo.UpdateUserAsync(user.Value);

            u.InviteRedeemUrl = user.Value.InviteRedeemUrl;
            // todo: figure out why this is happening
            await Task.Delay(5000);

            await _graph.AddUserToRole(inviteResult.InvitedUserId, isCustomerAdmin);

            return(user.Success ? new AppUser(user.Value) : u);
        }