github.com/infraboard/keyauth@v0.8.1/apps/session/impl/query.go (about)

     1  package impl
     2  
     3  import (
     4  	"go.mongodb.org/mongo-driver/bson"
     5  	"go.mongodb.org/mongo-driver/mongo/options"
     6  
     7  	"github.com/infraboard/keyauth/apps/session"
     8  	"github.com/infraboard/keyauth/apps/token"
     9  	"github.com/infraboard/mcube/exception"
    10  )
    11  
    12  func newQueryLoginLogRequest(req *session.QuerySessionRequest) (*querySessionRequest, error) {
    13  	if err := req.Validate(); err != nil {
    14  		return nil, err
    15  	}
    16  
    17  	return &querySessionRequest{
    18  		QuerySessionRequest: req,
    19  	}, nil
    20  }
    21  
    22  type querySessionRequest struct {
    23  	*session.QuerySessionRequest
    24  }
    25  
    26  func (r *querySessionRequest) FindOptions() *options.FindOptions {
    27  	pageSize := int64(r.Page.PageSize)
    28  	skip := int64(r.Page.PageSize) * int64(r.Page.PageNumber-1)
    29  
    30  	opt := &options.FindOptions{
    31  		Sort:  bson.D{{Key: "login_at", Value: -1}},
    32  		Limit: &pageSize,
    33  		Skip:  &skip,
    34  	}
    35  
    36  	return opt
    37  }
    38  
    39  func (r *querySessionRequest) FindFilter() bson.M {
    40  	filter := bson.M{
    41  		"domain": r.Domain,
    42  	}
    43  
    44  	if r.Account != "" {
    45  		filter["account"] = r.Account
    46  	}
    47  
    48  	if r.ApplicationId != "" {
    49  		filter["application_id"] = r.ApplicationId
    50  	}
    51  
    52  	if r.LoginIp != "" {
    53  		filter["login_ip"] = r.LoginIp
    54  	}
    55  
    56  	if r.LoginCity != "" {
    57  		filter["city"] = r.LoginCity
    58  	}
    59  
    60  	if r.GrantType != token.GrantType_NULL {
    61  		filter["grant_type"] = r.GrantType
    62  	}
    63  
    64  	loginAt := bson.A{}
    65  	if r.StartLoginTime != 0 {
    66  		loginAt = append(loginAt, bson.M{"login_at": bson.M{"$gte": r.StartLoginTime}})
    67  	}
    68  
    69  	if r.EndLoginTime != 0 {
    70  		loginAt = append(loginAt, bson.M{"login_at": bson.M{"$lte": r.EndLoginTime}})
    71  	}
    72  	if len(loginAt) > 0 {
    73  		filter["$and"] = loginAt
    74  	}
    75  
    76  	return filter
    77  }
    78  
    79  func newDescribeSession(req *session.DescribeSessionRequest) (*describeSessionRequest, error) {
    80  	if err := req.Validate(); err != nil {
    81  		return nil, exception.NewBadRequest(err.Error())
    82  	}
    83  
    84  	return &describeSessionRequest{req}, nil
    85  }
    86  
    87  type describeSessionRequest struct {
    88  	*session.DescribeSessionRequest
    89  }
    90  
    91  func (r *describeSessionRequest) FindOptions() *options.FindOneOptions {
    92  	opt := &options.FindOneOptions{
    93  		Sort: bson.D{{Key: "login_at", Value: -1}},
    94  	}
    95  
    96  	return opt
    97  }
    98  
    99  func (r *describeSessionRequest) FindFilter() bson.M {
   100  	filter := bson.M{}
   101  
   102  	if r.SessionId != "" {
   103  		filter["_id"] = r.SessionId
   104  	}
   105  	if r.Domain != "" {
   106  		filter["domain"] = r.Domain
   107  	}
   108  	if r.Account != "" {
   109  		filter["account"] = r.Account
   110  	}
   111  	if r.Login {
   112  		filter["logout_at"] = 0
   113  	}
   114  
   115  	return filter
   116  }
   117  
   118  func newQueryUserLastSessionRequest(req *session.QueryUserLastSessionRequest) (*queryUserLastSessionRequest, error) {
   119  	if err := req.Validate(); err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return &queryUserLastSessionRequest{
   124  		QueryUserLastSessionRequest: req,
   125  		pageSize:                    1,
   126  	}, nil
   127  }
   128  
   129  type queryUserLastSessionRequest struct {
   130  	pageSize int64
   131  	*session.QueryUserLastSessionRequest
   132  }
   133  
   134  func (r *queryUserLastSessionRequest) FindOptions() *options.FindOneOptions {
   135  	opt := &options.FindOneOptions{
   136  		Sort: bson.D{{Key: "login_at", Value: -1}},
   137  	}
   138  
   139  	return opt
   140  }
   141  
   142  func (r *queryUserLastSessionRequest) FindFilter() bson.M {
   143  	filter := bson.M{}
   144  
   145  	if r.Account != "" {
   146  		filter["account"] = r.Account
   147  	}
   148  
   149  	return filter
   150  }