Esempio n. 1
0
        public async Task CreateUser()
        {
            string password = RandomExtensions.NextString(10);
            UserVm user     = new UserVm
            {
                NameFirst  = RandomExtensions.NextString(20),
                NameSecond = RandomExtensions.NextString(20),
                About      = RandomExtensions.NextString(50),
                Photo      = RandomExtensions.NextString(100),
                Phones     = new List <UserPhoneVm>
                {
                    new UserPhoneVm
                    {
                        CountryCode      = 7,
                        SubscriberNumber = 9131743886,
                        IsMain           = true,
                        UserId           = 1000
                    }
                },
                Emails = new List <string>
                {
                    "*****@*****.**"
                }
            };
            var actualUser = await createUsersService.CreateNewUserAsync(user, 1, true, password);

            Assert.Equal(user.NameFirst, actualUser.FirstValue.NameFirst);
            Assert.Equal(user.NameSecond, actualUser.FirstValue.NameSecond);
            Assert.Equal(user.About, actualUser.FirstValue.About);
            Assert.Equal(user.Photo, actualUser.FirstValue.Photo);
            Assert.Equal(user.Phones.FirstOrDefault().FullNumber, actualUser.FirstValue.Phones.FirstOrDefault().FullNumber);
            Assert.Equal(user.Emails, actualUser.FirstValue.Emails);
            Assert.Equal(password, actualUser.SecondValue);
        }
Esempio n. 2
0
        public async Task <IActionResult> Create([FromForm] CreateUserModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _createUsersService.CreateNewUserAsync(
                    new UserVm
                {
                    NameFirst  = model.NameFirst,
                    NameSecond = model.NameSecond,
                    Phones     = !string.IsNullOrEmpty(model.PhoneNumber)
                            ? new List <UserPhoneVm>
                    {
                        new UserPhoneVm
                        {
                            FullNumber = model.PhoneNumber,
                            IsMain     = true
                        }
                    }
                            : null,
                    Emails = !string.IsNullOrEmpty(model.Email)
                            ? new List <string> {
                        model.Email
                    }
                            : null
                },
                    NodeSettings.Configs.Node.Id,
                    NodeSettings.Configs.ConfirmUsers,
                    model.Password).ConfigureAwait(false);

                var segment = await BlockSegmentsService.Instance.CreateNewUserSegmentAsync(
                    result.FirstValue,
                    NodeSettings.Configs.Node.Id,
                    NodeData.Instance.NodeKeys.SignPrivateKey,
                    NodeData.Instance.NodeKeys.SymmetricKey,
                    NodeData.Instance.NodeKeys.Password,
                    NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

                BlockGenerationHelper.Instance.AddSegment(segment);
                ShortUser shortUser = new ShortUser
                {
                    UserId      = result.FirstValue.Id.GetValueOrDefault(),
                    PrivateData = segment.PrivateData
                };
                _nodeNoticeService.SendNewUsersNodeNoticeAsync(shortUser, segment);
                return(Redirect(nameof(Index)));
            }
            return(BadRequest());
        }
Esempio n. 3
0
        public async Task <Response> CreateResponseAsync()
        {
            if (!NodeSettings.Configs.Node.UserRegistrationAllowed)
            {
                return(new ResultResponse(request.RequestId, "User registration is not allowed.", ErrorCode.PermissionDenied));
            }
            UserVm user = request.User;

            try
            {
                VerificationCodeInfo verificationCode = null;
                if (!request.User.Phones.IsNullOrEmpty() && request.User.Emails.IsNullOrEmpty())
                {
                    if (NodeSettings.Configs.Node.RegistrationMethod == RegistrationMethod.EmailRequired)
                    {
                        var errorObject = new
                        {
                            Email = "Email required"
                        };
                        return(new ResultResponse(request.RequestId, ObjectSerializer.ObjectToJson(errorObject), ErrorCode.WrongArgumentError));
                    }
                    if (await loadUsersService.IsPhoneExistsAsync(request.User.Phones.FirstOrDefault().FullNumber).ConfigureAwait(false))
                    {
                        var errorObject = new
                        {
                            Phone = "Phone already exists"
                        };
                        return(new ResultResponse(request.RequestId, ObjectSerializer.ObjectToJson(errorObject), ErrorCode.WrongArgumentError));
                    }
                    verificationCode = await verificationCodesService.GetUserVerificationCodeAsync(request.User.Phones.FirstOrDefault().FullNumber).ConfigureAwait(false);
                }
                else if (request.User.Phones.IsNullOrEmpty() && !request.User.Emails.IsNullOrEmpty())
                {
                    if (NodeSettings.Configs.Node.RegistrationMethod == RegistrationMethod.PhoneRequired)
                    {
                        var errorObject = new
                        {
                            Email = "Phone required"
                        };
                        return(new ResultResponse(request.RequestId, ObjectSerializer.ObjectToJson(errorObject), ErrorCode.WrongArgumentError));
                    }
                    if (await loadUsersService.IsEmailExistsAsync(request.User.Emails.FirstOrDefault()).ConfigureAwait(false))
                    {
                        var errorObject = new
                        {
                            Email = "Email already exists."
                        };
                        return(new ResultResponse(request.RequestId, ObjectSerializer.ObjectToJson(errorObject), ErrorCode.WrongArgumentError));
                    }
                    verificationCode = await verificationCodesService.GetUserVerificationCodeAsync(request.User.Emails.FirstOrDefault()).ConfigureAwait(false);
                }
                else
                {
                    if (NodeSettings.Configs.Node.RegistrationMethod != RegistrationMethod.NothingRequired)
                    {
                        var errorObject = new
                        {
                            Email = "Email only or phone only",
                            Phone = "Email only or phone only"
                        };
                        return(new ResultResponse(request.RequestId, ObjectSerializer.ObjectToJson(errorObject), ErrorCode.WrongArgumentError));
                    }
                }
                if (verificationCode != null &&
                    verificationCode.VCode != request.VCode &&
                    (!request.User.Emails.IsNullOrEmpty() || !request.User.Phones.IsNullOrEmpty()))
                {
                    var errorObject = new
                    {
                        VCode = "Wrong verification code"
                    };
                    return(new ResultResponse(request.RequestId, ObjectSerializer.ObjectToJson(errorObject), ErrorCode.WrongVerificationCode));
                }
                ValuePair <UserVm, string> userPasswordPair = await createUsersService.CreateNewUserAsync(user, NodeSettings.Configs.Node.Id, NodeSettings.Configs.ConfirmUsers).ConfigureAwait(false);

                TokenVm tempTokens = await tokensService.CreateTokenPairByUserIdAsync(userPasswordPair.FirstValue.Id.GetValueOrDefault(), false, 30 * 60).ConfigureAwait(false);

                clientConn.FileAccessToken = RandomExtensions.NextString(64);
                BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateNewUserSegmentAsync(
                    userPasswordPair.FirstValue,
                    NodeSettings.Configs.Node.Id,
                    NodeData.Instance.NodeKeys.SignPrivateKey,
                    NodeData.Instance.NodeKeys.SymmetricKey,
                    NodeData.Instance.NodeKeys.Password,
                    NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

                BlockGenerationHelper.Instance.AddSegment(segment);
                ShortUser shortUser = new ShortUser
                {
                    UserId      = userPasswordPair.FirstValue.Id.GetValueOrDefault(),
                    PrivateData = segment.PrivateData
                };
                nodeNoticeService.SendNewUsersNodeNoticeAsync(shortUser, segment);
                return(new TokensResponse(
                           request.RequestId,
                           userPasswordPair.FirstValue,
                           tempTokens.AccessToken,
                           tempTokens.RefreshToken,
                           clientConn.FileAccessToken,
                           userPasswordPair.SecondValue));
            }
            catch (CreateNewUserException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, ex.Message, ErrorCode.UnknownError));
            }
        }