2020-05-18 12:06:36 +02:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2021-12-06 16:28:49 +01:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
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"
|
2020-05-18 12:06:36 +02:00
|
|
|
)
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func UserSessionByIDs(db *gorm.DB, table, agentID, userID, instanceID string) (*model.UserSessionView, error) {
|
2020-05-18 12:06:36 +02:00
|
|
|
userSession := new(model.UserSessionView)
|
|
|
|
userAgentQuery := model.UserSessionSearchQuery{
|
2020-06-23 14:47:47 +02:00
|
|
|
Key: usr_model.UserSessionSearchKeyUserAgentID,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-05-18 12:06:36 +02:00
|
|
|
Value: agentID,
|
|
|
|
}
|
|
|
|
userQuery := model.UserSessionSearchQuery{
|
2020-06-23 14:47:47 +02:00
|
|
|
Key: usr_model.UserSessionSearchKeyUserID,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-05-18 12:06:36 +02:00
|
|
|
Value: userID,
|
|
|
|
}
|
2022-04-19 08:26:12 +02:00
|
|
|
instanceIDQuery := &model.UserSessionSearchQuery{
|
|
|
|
Key: usr_model.UserSessionSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, userAgentQuery, userQuery, instanceIDQuery)
|
2020-05-18 12:06:36 +02:00
|
|
|
err := query(db, userSession)
|
2020-07-06 13:18:10 +02:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-NGBs1", "Errors.UserSession.NotFound")
|
|
|
|
}
|
2020-05-18 12:06:36 +02:00
|
|
|
return userSession, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func UserSessionsByUserID(db *gorm.DB, table, userID, instanceID string) ([]*model.UserSessionView, error) {
|
2020-06-05 07:50:04 +02:00
|
|
|
userSessions := make([]*model.UserSessionView, 0)
|
|
|
|
userAgentQuery := &usr_model.UserSessionSearchQuery{
|
2020-06-23 14:47:47 +02:00
|
|
|
Key: usr_model.UserSessionSearchKeyUserID,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-06-05 07:50:04 +02:00
|
|
|
Value: userID,
|
|
|
|
}
|
2022-04-19 08:26:12 +02:00
|
|
|
instanceIDQuery := &usr_model.UserSessionSearchQuery{
|
|
|
|
Key: usr_model.UserSessionSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2020-06-25 08:01:13 +02:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSessionSearchRequest{
|
2022-04-19 08:26:12 +02:00
|
|
|
Queries: []*usr_model.UserSessionSearchQuery{userAgentQuery, instanceIDQuery},
|
2020-06-05 07:50:04 +02:00
|
|
|
})
|
|
|
|
_, err := query(db, &userSessions)
|
|
|
|
return userSessions, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func UserSessionsByAgentID(db *gorm.DB, table, agentID, instanceID string) ([]*model.UserSessionView, error) {
|
2020-05-18 12:06:36 +02:00
|
|
|
userSessions := make([]*model.UserSessionView, 0)
|
|
|
|
userAgentQuery := &usr_model.UserSessionSearchQuery{
|
2020-06-23 14:47:47 +02:00
|
|
|
Key: usr_model.UserSessionSearchKeyUserAgentID,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-05-18 12:06:36 +02:00
|
|
|
Value: agentID,
|
|
|
|
}
|
2022-04-19 08:26:12 +02:00
|
|
|
instanceIDQuery := &usr_model.UserSessionSearchQuery{
|
|
|
|
Key: usr_model.UserSessionSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2020-06-25 08:01:13 +02:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSessionSearchRequest{
|
2022-04-19 08:26:12 +02:00
|
|
|
Queries: []*usr_model.UserSessionSearchQuery{userAgentQuery, instanceIDQuery},
|
2020-05-18 12:06:36 +02:00
|
|
|
})
|
2020-06-05 07:50:04 +02:00
|
|
|
_, err := query(db, &userSessions)
|
2020-05-18 12:06:36 +02:00
|
|
|
return userSessions, err
|
|
|
|
}
|
|
|
|
|
2022-06-03 14:37:24 +02:00
|
|
|
func UserSessionsByOrgID(db *gorm.DB, table, orgID, instanceID string) ([]*model.UserSessionView, error) {
|
|
|
|
userSessions := make([]*model.UserSessionView, 0)
|
|
|
|
userAgentQuery := &usr_model.UserSessionSearchQuery{
|
|
|
|
Key: usr_model.UserSessionSearchKeyResourceOwner,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: orgID,
|
|
|
|
}
|
|
|
|
instanceIDQuery := &usr_model.UserSessionSearchQuery{
|
|
|
|
Key: usr_model.UserSessionSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSessionSearchRequest{
|
|
|
|
Queries: []*usr_model.UserSessionSearchQuery{userAgentQuery, instanceIDQuery},
|
|
|
|
})
|
|
|
|
_, err := query(db, &userSessions)
|
|
|
|
return userSessions, err
|
|
|
|
}
|
|
|
|
|
2021-12-06 16:28:49 +01:00
|
|
|
func ActiveUserSessions(db *gorm.DB, table string) (uint64, error) {
|
2020-12-02 08:50:59 +01:00
|
|
|
activeQuery := &usr_model.UserSessionSearchQuery{
|
|
|
|
Key: usr_model.UserSessionSearchKeyState,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodEquals,
|
2022-03-31 11:36:26 +02:00
|
|
|
Value: domain.UserSessionStateActive,
|
2020-12-02 08:50:59 +01:00
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSessionSearchRequest{
|
|
|
|
Queries: []*usr_model.UserSessionSearchQuery{activeQuery},
|
|
|
|
})
|
2021-12-06 16:28:49 +01:00
|
|
|
return query(db, nil)
|
2020-12-02 08:50:59 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-26 13:01:37 +02:00
|
|
|
func PutUserSessions(db *gorm.DB, table string, sessions ...*model.UserSessionView) error {
|
|
|
|
save := repository.PrepareBulkSave(table)
|
|
|
|
s := make([]interface{}, len(sessions))
|
|
|
|
for i, session := range sessions {
|
|
|
|
s[i] = session
|
|
|
|
}
|
|
|
|
return save(db, s...)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|