# Module: Tglogin

TODO: ALL SIGNUP NEEDS VALIDATION

# Child modules and classes

Module Tglogin::Signup

# Public Class Methods

### askhint (login)

Show source...

         # File bbs/lib/tglogin.rb, line 114
    114:   def Tglogin::askhint(login)
    115:     question = User[:login => login].pwhint_question
    116:     t = Tgtemplate::Template.new('login_prompt_pwhint.ftl')
    117:     t.render("pwhint_question"=>question)
    118:     answer = Tgio::Input::inputform(75)
    119:     dbanswer   = User[:login => login].pwhint_answer
    120:
    121:     if answer == dbanswer
    122:       puts "VALIDATED - CLEARING LOGIN FAILURES"
    123:       User.clearfailed(login)
    124:       return true
    125:     else
    126:       puts "INCORRECT"
    127:       return false
    128:     end
    129:
    130:   end

### auth ()

This function provides the entire login routine logic. Configuration variables apply.

Show source...

         # File bbs/lib/tglogin.rb, line 133
    133:   def Tglogin::auth
    134:     self.banner
    135:     attempt = 0
    136:     while attempt < @logincfg['attempts'] do
    137:       login = self.userprompt
    138:       unless login.nil?
    139:         password = self.passwordprompt
    140:         thistry = User.authorize(login, password)
    141:         case thistry[:result]
    142:           # If user is validated and allowed, then return true and create a session.
    143:           when "true"
    144:             User.countlogin(login)
    145:             User.clearfailed(login)
    146:             user = User[:login => login]
    147:             group = Group[:id => user.group_id]
    148:
    149:             # Create caller history entry & update session
    150:             $callerid = Tgcallhistory.create(
    151:                     :user_id => user.id,
    152:                     :alias  => user.login,
    153:                     :time_login => Time.now
    154:                     )
    155:
    156:             timeremain = (group.dailytimelimit - $callerid.thisuser_time_today)
    157:
    158:             # Create global session variable
    159:             $session = Session.new
    160:                 $session.user_id    = user.id
    161:                 $session.group_id   = user.group_id
    162:                 $session.level      = group.level
    163:                 $session.created    = Time.now
    164:                 $session.expires    = Time.now + 1.hours
    165:                 $session.caller_id  = $callerid.id
    166:                 $session.filearea   = nil # Set the filearea tracking to nil. Initialized in FileAreaController.menu
    167:                 $session.msgarea    = nil  # Set the filearea tracking to nil. Initialized in MsgAreaController.menu
    168:                 $session.chatroom   = nil  # Set the filearea tracking to nil. Initialized in ChatRoomController.menu
    169:                 $session.expires    = Time.now + timeremain.minutes
    170:                 # User preferences go into session so we dont have to query the DB every time.
    171:                 $session.pref_show_menus = user.pref_show_menus
    172:                 $session.pref_term_pager = user.pref_term_pager
    173:             $session.save
    174:             # TODO: BUG#:  581982 - $session does not seem to know its record ID in the table on the very first login only.
    175:             # TODO: Test if retrieving the saved record back as a dataset will solve this problem.
    176:
    177:
    178:             if timeremain <= 0
    179:               Tgtemplate::display('login_dailylimit.ftl')
    180:               Telegard::goodbye_fast
    181:             else
    182:               Tgtemplate::display('login_session_info.ftl', {
    183:                       'timetoday' => $callerid.thisuser_time_today.to_s,
    184:                       'grouplimit'=> group.dailytimelimit.to_s,
    185:                       'logintoday'=> $callerid.thisuser_logincount_today.to_s,
    186:                       'curtime'   => Time.now.to_s,
    187:                       'expires'   => $session.expires.to_s,
    188:                       'timeremain'=> timeremain })
    189:
    190:             end
    191:             return true
    192:             break
    193:           when "false"
    194:             attempt += 1
    195:             Tgtemplate::display('login_failed.ftl', {"attempt" => "#{attempt.to_s}", "maxattempts" => "#{@logincfg['attempts'].to_s}","failures"=> thistry[:failures].to_s})
    196:             # Test if user.login_failures is equal to 1 less than the configured lockout.
    197:             # If so, prompt user for PWHINT if configured
    198:             if thistry[:failures] == @logincfg['lockout'] && @logincfg['usehint'] == true
    199:               self.askhint(login)
    200:             end
    201:           # -> Chances are, we will NEVER get to this ' when "invalid" ' clause. But if for some reason we do, handle it.
    202:           when "invalid"
    203:             attempt += 1
    204:             print "\n"
    205:             Tgtemplate::display('login_invalid.ftl', tvars = {"attempt" => "#{attempt.to_s}", "maxattempts" => "#{@logincfg['attempts'].to_s}"})
    206:           when "locked"
    207:             print "\n"
    208:             Tgtemplate::display('login_locked_user.ftl', {"attempt" => "#{attempt.to_s}", "maxattempts" => "#{@logincfg['attempts'].to_s}"})
    209:         end #/case
    210:       else # If login is NIL, its an invalid login. Do some stuff here  (Same as case when invalid above)
    211:         attempt += 1
    212:         print "\n"
    213:         Tgtemplate::display('login_invalid.ftl', {"attempt" => "#{attempt.to_s}", "maxattempts" => "#{@logincfg['attempts'].to_s}", "allownew" => "#{@logincfg['allownew']}"})
    214:       end #/unless login.nil?
    215:     end #/while
    216:     # Return nil as a failsafe.
    217:     return nil
    218:   end

### banner ()

Displays the template for the login banner.

Show source...

        # File bbs/lib/tglogin.rb, line 75
    75:   def Tglogin::banner
    76:     Tgtemplate::display('login_banner.ftl', {"allownew" => @logincfg['allownew'].to_s})
    77:   end

### cryptpassword (cleartxt)

Encrypt the clear text password

Show source...

         # File bbs/lib/tglogin.rb, line 109
    109:   def Tglogin::cryptpassword(cleartxt)
    110:     password = BCrypt::Password.create(cleartxt)
    111:     return password
    112:   end

### passwordprompt ()

Password prompt - displays template, calls highline ask and returns crypted password.

Show source...

         # File bbs/lib/tglogin.rb, line 101
    101:   def Tglogin::passwordprompt
    102:     t = Tgtemplate::Template.new('login_prompt_password.ftl')
    103:     t.render()
    104:     cleartxt = Tgio::Input::passwordform
    105:     return cleartxt
    106:   end

### signup ()

Signup routine for New Users

Show source...

         # File bbs/lib/tglogin.rb, line 221
    221:   def Tglogin::signup
    222:     cfg = $cfg['signup']
    223:     Tgio::ansiclear
    224:     JLine::ConsoleReader.new.set_use_pagination(true)
    225:     Tgtemplate::display('signup_instructions.ftl', {"bbs"=>$cfg['bbs']})
    226:     unless Tgio::Input::inputyn('signup_askcontinue.ftl') == true
    227:       puts "Goodbye!"
    228:       exit 0
    229:     end
    230:
    231:     Tgtemplate::display('signup_aup.ftl', {"bbs"=>$cfg['bbs']})
    232:     if Tgio::Input::inputyn('signup_accept_aup.ftl') == true
    233:       # Ask required questions
    234:       login     = Tglogin::Signup::asklogin
    235:       firstname = Tglogin::Signup::askfirstname
    236:       lastname  = Tglogin::Signup::asklastname
    237:       email     = Tglogin::Signup::askemail
    238:       password  = Tglogin::Signup::askpassword
    239:       pwhint_question  = Tglogin::Signup::askpwhint_question
    240:       pwhint_answer    = Tglogin::Signup::askpwhint_answer
    241:       address1  = Tglogin::Signup::askaddress1
    242:       address2  = Tglogin::Signup::askaddress2
    243:       city      = Tglogin::Signup::askcity
    244:       state     = Tglogin::Signup::askstate
    245:       postal    = Tglogin::Signup::askpostal
    246:       country   = Tglogin::Signup::askcountry
    247:       phone     = Tglogin::Signup::askphone
    248:       gender    = Tglogin::Signup::askgender
    249:       bday      = Tglogin::Signup::askbday
    250:       custom1   = Tglogin::Signup::askcustom(1) if cfg['ask_custom1'] == true
    251:       custom2   = Tglogin::Signup::askcustom(2) if cfg['ask_custom2'] == true
    252:       custom3   = Tglogin::Signup::askcustom(3) if cfg['ask_custom3'] == true
    253:
    254:       # Send to review screen
    255:       signupdata = {
    256:         "login" => login,
    257:         "password" => password,
    258:         "pwhint_question" => pwhint_question,
    259:         "pwhint_answer" => pwhint_answer,
    260:         "firstname" => firstname,
    261:         "lastname" => lastname,
    262:         "email" => email,
    263:         "city" => city,
    264:         "state" => state,
    265:         "country" => country,
    266:         "address1" => address1,
    267:         "address2" => address2,
    268:         "postal" => postal,
    269:         "phone" => phone,
    270:         "gender" => gender,
    271:         "bday" => bday.to_s,
    272:         "custom1" => custom1,
    273:         "custom2" => custom2,
    274:         "custom3" => custom3,
    275:         "pwexpires" => (Time.now + 90.days).to_s
    276:       }
    277:       Tglogin::Signup::confirm(signupdata)
    278:     else
    279:       puts "Goodybye!"
    280:       exit 0
    281:     end
    282:     # Send login name back to login.
    283:     return login
    284:   end

### userprompt ()

Username prompt - displays template, calls highline ask and returns username.

Show source...

        # File bbs/lib/tglogin.rb, line 80
    80:   def Tglogin::userprompt
    81:     Tgtemplate::display('login_prompt_user.ftl')
    82:     username = Tgio::Input::loginform
    83:     if username.upcase == "NEW"
    84:       if @logincfg['allownew'] == true
    85:         username = self.signup
    86:       else
    87:         Tgtemplate::display('login_new_disabled.ftl')
    88:         return nil
    89:       end
    90:     end
    91:     # check if the username exists in the DB so as not to waste resources on nonexistent accounts.
    92:     if User.exists?(username) == true
    93:       return username
    94:     else
    95:
    96:       return nil
    97:     end
    98:   end