Store also username in database.

This commit is contained in:
Otto Hollmann 2022-05-11 06:20:11 +02:00
parent cf358a572c
commit e849d51761
No known key found for this signature in database
GPG Key ID: 101D022F467ECE22
4 changed files with 31 additions and 26 deletions

View File

@ -284,9 +284,8 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
} else {
leavePortals(ce)
}
var pass string;
ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass)
if ret && pass != "" {
ret := ce.User.bridge.DB.User.GetCredentialsByMXID(ce.User.MXID, &password, &username)
if ret && password != "" {
ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.")
}
}
@ -306,7 +305,7 @@ func (handler *CommandHandler) CommandSavePassword(ce *CommandEvent) {
return
}
ret = ce.User.bridge.DB.User.SetPassByMXID(ce.User.Conn.LoginInfo.Password, ce.User.MXID)
ret = ce.User.bridge.DB.User.SetCredentialsByMXID(ce.User.Conn.LoginInfo.Password, ce.User.Conn.LoginInfo.Username, ce.User.MXID)
if ret == true {
ce.Reply("Your password was successfully saved into database.")
} else {
@ -324,7 +323,7 @@ func (handler *CommandHandler) CommandRemovePassword(ce *CommandEvent) {
return
}
ret = ce.User.bridge.DB.User.SetPassByMXID("", ce.User.MXID)
ret = ce.User.bridge.DB.User.SetCredentialsByMXID("", "", ce.User.MXID)
if ret == true {
ce.Reply("Your password was successfully removed from database.")
} else {
@ -389,9 +388,10 @@ func (handler *CommandHandler) CommandPing(ce *CommandEvent) {
}
ce.Reply("You're logged in as @" + username + ", orgid is " + orgId)
}
var pass string;
ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass)
if ret && pass != "" {
var password string;
var username string;
ret := ce.User.bridge.DB.User.GetCredentialsByMXID(ce.User.MXID, &password, &username)
if ret && password != "" {
ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.")
}
}

View File

@ -5,7 +5,7 @@ import (
)
func init() {
upgrades[20] = upgrade{"Add password column to user table.", func(tx *sql.Tx, c context) error {
upgrades[20] = upgrade{"Add password and username column to user table.", func(tx *sql.Tx, c context) error {
if c.dialect == Postgres {
_, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN password VARCHAR(255)`)
if err != nil {
@ -13,6 +13,13 @@ func init() {
}
}
if c.dialect == Postgres {
_, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN username VARCHAR(255)`)
if err != nil {
return err
}
}
return nil
}}
}

View File

@ -54,17 +54,17 @@ func (uq *UserQuery) GetByJID(userID types.SkypeID) *User {
return uq.New().Scan(row)
}
func (uq *UserQuery) GetPassByMXID(userID id.UserID, password *string) bool {
row := uq.db.QueryRow(`SELECT password FROM "user" WHERE mxid=$1`, userID)
func (uq *UserQuery) GetCredentialsByMXID(userID id.UserID, password *string, username *string) bool {
row := uq.db.QueryRow(`SELECT password, username FROM "user" WHERE mxid=$1`, userID)
if row == nil {
return false
}
err := row.Scan(password)
err := row.Scan(password, username)
return err == nil
}
func (uq *UserQuery) SetPassByMXID(password string, userID id.UserID) bool {
row := uq.db.QueryRow(`UPDATE "user" SET password=$1 WHERE mxid=$2`, password, userID)
func (uq *UserQuery) SetCredentialsByMXID(password string, username string, userID id.UserID) bool {
row := uq.db.QueryRow(`UPDATE "user" SET password=$1, username=$2 WHERE mxid=$3`, password, username, userID)
return row != nil
}

22
user.go
View File

@ -295,8 +295,9 @@ func (user *User) Connect(evenIfNoSession bool) bool {
func (user *User) RestoreSession() bool {
if user.Session != nil {
var password string
ret := user.bridge.DB.User.GetPassByMXID(user.MXID, &password)
if ret && password != "" {
var username string
ret := user.bridge.DB.User.GetCredentialsByMXID(user.MXID, &password, &username)
if ret && password != "" && username != "" {
user.log.Debugln("Found password for user " + user.MXID + " in database, trying to login.")
ce := &CommandEvent{
Bot: user.bridge.MatrixHandler.cmd.bridge.Bot,
@ -305,17 +306,13 @@ func (user *User) RestoreSession() bool {
RoomID: user.GetManagementRoom(),
User: user,
}
username := strings.Split(strings.Replace(user.JID, skypeExt.NewUserSuffix, "", 1), ":")
strings.Split(user.JID, ":")
if username[1] != "" {
err := user.Login(ce, username[1], password)
if err == nil {
user.log.Debugln("User " + username[1] + " successfully connected.")
syncAll(user, false)
}
} else {
user.log.Debugln("Unable to get username for user %s , user.JID=%s", user.MXID, user.JID)
err := user.Login(ce, username, password)
if err == nil {
user.log.Debugln("User " + username + " successfully connected.")
syncAll(user, false)
}
} else {
user.log.Debugln("An error occured while obtaining username and password for user " + user.MXID + ".")
}
//sess, err := user.Conn.RestoreWithSession(*user.Session)
@ -432,6 +429,7 @@ func (user *User) monitorSession(ce *CommandEvent) {
leavePortals(ce)
}
} else {
ce.Reply("Session expired\nStore your password into database with command `save-password` to resolve this issue.")
close(user.Conn.Refresh)
leavePortals(ce)
}