Exemplo n.º 1
0
        /// <summary> Function holding user creation and initialization logic </summary>
        /// <param name="user"> Username to initialize </param>
        /// <param name="pass"> Password provided for creation </param>
        /// <param name="remoteIP"> IP of client </param>
        /// <returns> Created <see cref="UserLoginInfo"/> record </returns>
        private UserLoginInfo CreateNewUser(string user, string pass, string remoteIP)
        {
            if (remoteIP == null)
            {
                remoteIP = "--NULL--";
            }
            if (remoteIP == "")
            {
                remoteIP = "--EMPTY--";
            }
            // List<UserAccountCreation> accountCreations = dbService.GetAll<UserAccountCreation>(nameof(UserAccountCreation.ipAddress), remoteIP);
            var file = $"{remoteIP}.wtf";

            if (file.Contains(":"))
            {
                file = file.Replace(":", ".");
            }
            List <UserAccountCreation> accountCreations = accountCreationDB.Open(file);

            Log.Info($"Client at <{remoteIP}> has {accountCreations.Count} account creations.");
            if (accountCreations.Count > 5)
            {
                Log.Info($"Too many account creations!.");
                return(null);
            }

            if (userDB.Exists(user))
            {
                Log.Info($"Username {user} already taken, can't create a new user!!");
                return(null);
            }

            Guid          userId   = Guid.NewGuid();
            UserLoginInfo userInfo = new UserLoginInfo();

            userInfo.userName = user;
            string hash = Hash(pass);

            userInfo.hash      = hash;
            userInfo.guid      = userId;
            userInfo.lastLogin = DateTime.UtcNow;
            userDB.Save(user, userInfo);

            try {
                if (userInitializer == null)
                {
                    Log.Warning($"LoginService.CreateNewUser: No userInitializer found. Please set a function to set up a new user.");
                }
                Log.Info($"Creating new account for user {user} / {userId}");
                userInitializer?.Invoke(userId);

                accountCreations.Add(new UserAccountCreation()
                {
                    userName  = user,
                    ipAddress = remoteIP,
                    time      = DateTime.UtcNow
                });
                DB.Of <List <UserAccountCreation> > .Save(file, accountCreations);
            } catch (Exception e) {
                Log.Error($"Error initializing user {user} / {userId} ", e);
                return(null);
            }

            return(userInfo);
        }