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