2020-05-18 10:06:36 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2024-01-30 15:17:54 +00:00
|
|
|
"database/sql"
|
|
|
|
_ "embed"
|
|
|
|
"errors"
|
|
|
|
|
2021-12-06 15:28:49 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2024-01-30 15:17:54 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-04-26 23:01:45 +00: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 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
2024-01-30 15:17:54 +00:00
|
|
|
//go:embed user_session_by_id.sql
|
|
|
|
var userSessionByIDQuery string
|
2020-05-18 10:06:36 +00:00
|
|
|
|
2024-01-30 15:17:54 +00:00
|
|
|
//go:embed user_sessions_by_user_agent.sql
|
|
|
|
var userSessionsByUserAgentQuery string
|
2020-06-05 05:50:04 +00:00
|
|
|
|
2024-01-30 15:17:54 +00: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 10:06:36 +00:00
|
|
|
}
|
2024-01-30 15:17:54 +00: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 12:37:24 +00:00
|
|
|
return userSessions, err
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:06:36 +00:00
|
|
|
func PutUserSession(db *gorm.DB, table string, session *model.UserSessionView) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-05-18 10:06:36 +00:00
|
|
|
return save(db, session)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func DeleteUserSessions(db *gorm.DB, table, userID, instanceID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKeys(table,
|
2022-11-30 16:01:17 +00:00
|
|
|
repository.Key{Key: model.UserSessionSearchKey(usr_model.UserSessionSearchKeyUserID), Value: userID},
|
|
|
|
repository.Key{Key: model.UserSessionSearchKey(usr_model.UserSessionSearchKeyInstanceID), Value: instanceID},
|
2022-04-19 06:26:12 +00:00
|
|
|
)
|
2020-05-18 10:06:36 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|
2022-10-26 13:06:48 +00:00
|
|
|
|
|
|
|
func DeleteInstanceUserSessions(db *gorm.DB, table, instanceID string) error {
|
2022-11-30 16:01:17 +00: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 13:06:48 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|
2024-01-30 15:17:54 +00: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
|
|
|
|
}
|