2020-05-18 12:06:36 +02:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2024-01-30 16:17:54 +01:00
|
|
|
"database/sql"
|
|
|
|
_ "embed"
|
|
|
|
"errors"
|
|
|
|
|
2021-12-06 16:28:49 +01:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2024-01-30 16:17:54 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-04-27 01:01:45 +02:00
|
|
|
usr_model "github.com/zitadel/zitadel/internal/user/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/user/repository/view/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-05-18 12:06:36 +02:00
|
|
|
)
|
|
|
|
|
2024-01-30 16:17:54 +01:00
|
|
|
//go:embed user_session_by_id.sql
|
|
|
|
var userSessionByIDQuery string
|
2020-05-18 12:06:36 +02:00
|
|
|
|
2024-01-30 16:17:54 +01:00
|
|
|
//go:embed user_sessions_by_user_agent.sql
|
|
|
|
var userSessionsByUserAgentQuery string
|
2020-06-05 07:50:04 +02:00
|
|
|
|
2024-01-30 16:17:54 +01:00
|
|
|
func UserSessionByIDs(db *database.DB, agentID, userID, instanceID string) (userSession *model.UserSessionView, err error) {
|
|
|
|
err = db.QueryRow(
|
|
|
|
func(row *sql.Row) error {
|
|
|
|
userSession, err = scanUserSession(row)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
userSessionByIDQuery,
|
|
|
|
agentID,
|
|
|
|
userID,
|
|
|
|
instanceID,
|
|
|
|
)
|
|
|
|
return userSession, err
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
2024-01-30 16:17:54 +01:00
|
|
|
func UserSessionsByAgentID(db *database.DB, agentID, instanceID string) (userSessions []*model.UserSessionView, err error) {
|
|
|
|
err = db.Query(
|
|
|
|
func(rows *sql.Rows) error {
|
|
|
|
userSessions, err = scanUserSessions(rows)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
userSessionsByUserAgentQuery,
|
|
|
|
agentID,
|
|
|
|
instanceID,
|
|
|
|
)
|
2022-06-03 14:37:24 +02:00
|
|
|
return userSessions, err
|
|
|
|
}
|
|
|
|
|
2020-05-18 12:06:36 +02:00
|
|
|
func PutUserSession(db *gorm.DB, table string, session *model.UserSessionView) error {
|
2020-06-25 08:01:13 +02:00
|
|
|
save := repository.PrepareSave(table)
|
2020-05-18 12:06:36 +02:00
|
|
|
return save(db, session)
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func DeleteUserSessions(db *gorm.DB, table, userID, instanceID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKeys(table,
|
2022-11-30 17:01:17 +01:00
|
|
|
repository.Key{Key: model.UserSessionSearchKey(usr_model.UserSessionSearchKeyUserID), Value: userID},
|
|
|
|
repository.Key{Key: model.UserSessionSearchKey(usr_model.UserSessionSearchKeyInstanceID), Value: instanceID},
|
2022-04-19 08:26:12 +02:00
|
|
|
)
|
2020-05-18 12:06:36 +02:00
|
|
|
return delete(db)
|
|
|
|
}
|
2022-10-26 15:06:48 +02:00
|
|
|
|
|
|
|
func DeleteInstanceUserSessions(db *gorm.DB, table, instanceID string) error {
|
2022-11-30 17:01:17 +01:00
|
|
|
delete := repository.PrepareDeleteByKey(table,
|
|
|
|
model.UserSessionSearchKey(usr_model.UserSessionSearchKeyInstanceID),
|
|
|
|
instanceID,
|
|
|
|
)
|
|
|
|
return delete(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteOrgUserSessions(db *gorm.DB, table, instanceID, orgID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKeys(table,
|
|
|
|
repository.Key{Key: model.UserSessionSearchKey(usr_model.UserSessionSearchKeyResourceOwner), Value: orgID},
|
|
|
|
repository.Key{Key: model.UserSessionSearchKey(usr_model.UserSessionSearchKeyInstanceID), Value: instanceID},
|
|
|
|
)
|
2022-10-26 15:06:48 +02:00
|
|
|
return delete(db)
|
|
|
|
}
|
2024-01-30 16:17:54 +01:00
|
|
|
|
|
|
|
func scanUserSession(row *sql.Row) (*model.UserSessionView, error) {
|
|
|
|
session := new(model.UserSessionView)
|
|
|
|
var userName, loginName, displayName, avatarKey sql.NullString
|
|
|
|
err := row.Scan(
|
|
|
|
&session.CreationDate,
|
|
|
|
&session.ChangeDate,
|
|
|
|
&session.ResourceOwner,
|
|
|
|
&session.State,
|
|
|
|
&session.UserAgentID,
|
|
|
|
&session.UserID,
|
|
|
|
&userName,
|
|
|
|
&loginName,
|
|
|
|
&displayName,
|
|
|
|
&avatarKey,
|
|
|
|
&session.SelectedIDPConfigID,
|
|
|
|
&session.PasswordVerification,
|
|
|
|
&session.PasswordlessVerification,
|
|
|
|
&session.ExternalLoginVerification,
|
|
|
|
&session.SecondFactorVerification,
|
|
|
|
&session.SecondFactorVerificationType,
|
|
|
|
&session.MultiFactorVerification,
|
|
|
|
&session.MultiFactorVerificationType,
|
|
|
|
&session.Sequence,
|
|
|
|
&session.InstanceID,
|
|
|
|
)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, zerrors.ThrowNotFound(nil, "VIEW-NGBs1", "Errors.UserSession.NotFound")
|
|
|
|
}
|
|
|
|
session.UserName = userName.String
|
|
|
|
session.LoginName = loginName.String
|
|
|
|
session.DisplayName = displayName.String
|
|
|
|
session.AvatarKey = avatarKey.String
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func scanUserSessions(rows *sql.Rows) ([]*model.UserSessionView, error) {
|
|
|
|
sessions := make([]*model.UserSessionView, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
session := new(model.UserSessionView)
|
|
|
|
var userName, loginName, displayName, avatarKey sql.NullString
|
|
|
|
err := rows.Scan(
|
|
|
|
&session.CreationDate,
|
|
|
|
&session.ChangeDate,
|
|
|
|
&session.ResourceOwner,
|
|
|
|
&session.State,
|
|
|
|
&session.UserAgentID,
|
|
|
|
&session.UserID,
|
|
|
|
&userName,
|
|
|
|
&loginName,
|
|
|
|
&displayName,
|
|
|
|
&avatarKey,
|
|
|
|
&session.SelectedIDPConfigID,
|
|
|
|
&session.PasswordVerification,
|
|
|
|
&session.PasswordlessVerification,
|
|
|
|
&session.ExternalLoginVerification,
|
|
|
|
&session.SecondFactorVerification,
|
|
|
|
&session.SecondFactorVerificationType,
|
|
|
|
&session.MultiFactorVerification,
|
|
|
|
&session.MultiFactorVerificationType,
|
|
|
|
&session.Sequence,
|
|
|
|
&session.InstanceID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
session.UserName = userName.String
|
|
|
|
session.LoginName = loginName.String
|
|
|
|
session.DisplayName = displayName.String
|
|
|
|
session.AvatarKey = avatarKey.String
|
|
|
|
sessions = append(sessions, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, zerrors.ThrowInternal(err, "VIEW-FSF3g", "Errors.Query.CloseRows")
|
|
|
|
}
|
|
|
|
return sessions, nil
|
|
|
|
}
|