Пример #1
0
        /// <summary>
        /// User creation request processing.
        /// </summary>
        /// <param name="login">User login.</param>
        /// <param name="password">User password.</param>
        /// <param name="accessLevel">User account access level.</param>
        /// <returns>User account unique id.</returns>
        public override int User_Create( string login, string password, byte accessLevel )
        {
            MsSqlDataCommand msdc = new MsSqlDataCommand
                (
                    "[User_Create]",
                     CommandType.StoredProcedure,
                     new SqlParameter("@login", SqlDbType.VarChar, 0x10) { Value = login },
                     new SqlParameter("@password", SqlDbType.VarChar, 0x2f) { Value = password },
                     new SqlParameter("@access_level", SqlDbType.TinyInt) { Value = accessLevel },
                     new SqlParameter("@uid", SqlDbType.Int) { Direction = ParameterDirection.Output }
                );

            SetConnection(ref msdc, m_ActiveConnections.Next());
            msdc.ExecuteNonQuery();

            int uid = TypesConverter.GetInt(msdc.Parameters["@uid"].Value, int.MinValue);

            ReleaseCommand(msdc);

            return uid;
        }
Пример #2
0
        /// <summary>
        /// User authentication request processing.
        /// </summary>
        /// <param name="netRequest"><see cref="UserAuthenticationRequest"/> to verify user data from.</param>
        /// <param name="settings"><see cref="LoginServiceSettings"/> object.</param>
        /// <returns><see cref="UserAuthenticationResponse"/> struct.</returns>
        public override UserAuthenticationResponse User_Auth( UserAuthenticationRequest netRequest, LoginServiceSettings settings )
        {
            MsSqlDataCommand msdc = new MsSqlDataCommand
            (
                "[User_Auth]",
                CommandType.StoredProcedure,
                new SqlParameter("@login", SqlDbType.VarChar, 0x10) { Value = netRequest.Login },
                new SqlParameter("@password", SqlDbType.VarChar, 0x2f) { Value = netRequest.Password },
                new SqlParameter("@uid", SqlDbType.Int) { Direction = ParameterDirection.Output },
                new SqlParameter("@last_world", SqlDbType.TinyInt) { Direction = ParameterDirection.Output },
                new SqlParameter("@access_level", SqlDbType.TinyInt) { Direction = ParameterDirection.Output }
            );

            SetConnection(ref msdc, m_ActiveConnections.Next());

            msdc.ExecuteNonQuery();

            int uid = TypesConverter.GetInt(msdc.Parameters["@uid"].Value, int.MinValue);

            UserAuthenticationResponse rsp = new UserAuthenticationResponse(netRequest.RequestID, UserAuthenticationResponseType.UserOrPasswordWrong);

            switch ( uid )
            {
                case int.MinValue: // conversion error
                    {
                        rsp.Response = UserAuthenticationResponseType.SystemError;
                        break;
                    }
                case -2: // invalid credentials
                    break;
                case -1: // login doesn't exist
                    {
                        if ( settings != null && settings.AutoCreateUser )
                        {
                            // creating user
                            uid = User_Create(netRequest.Login, netRequest.Password, settings.DefaultAccessLevel);

                            switch ( uid )
                            {
                                case int.MinValue: // conversion error
                                case -2: // db insert error
                                    {
                                        rsp.Response = UserAuthenticationResponseType.SystemError;
                                        break;
                                    }
                                case -1: // login already exists
                                    break;
                                default: // user created
                                    {
                                        rsp.Response = UserAuthenticationResponseType.UserAccepted;
                                        rsp.UserID = uid;
                                        rsp.AccessLevel = settings.DefaultAccessLevel;
                                        rsp.LastWorldID = TypesConverter.GetByte(msdc.Parameters["@last_world"].Value, 1);
                                        break;
                                    }
                            }
                        }
                        else
                            goto case -2;
                        break;
                    }
                default:
                    {
                        rsp.Response = UserAuthenticationResponseType.UserAccepted;
                        rsp.UserID = uid;
                        rsp.LastWorldID = TypesConverter.GetByte(msdc.Parameters["@last_world"].Value, 1);
                        rsp.AccessLevel = TypesConverter.GetByte(msdc.Parameters["@access_level"].Value, 0);
                        break;
                    }
            }

            ReleaseCommand(msdc);

            return rsp;
        }