package admin import ( "database/sql" "errors" "fmt" "jiacrontab/models" "jiacrontab/pkg/proto" "jiacrontab/pkg/util" "jiacrontab/pkg/version" "strings" "time" jwt "github.com/dgrijalva/jwt-go" ) type CustomerClaims struct { jwt.StandardClaims Version int64 UserID uint Mail string Username string GroupID uint Root bool } // Login 用户登录 func Login(ctx *myctx) { var ( err error reqBody LoginReqParams user models.User customerClaims CustomerClaims cfg = ctx.adm.getOpts() ) if err = ctx.Valid(&reqBody); err != nil { ctx.respParamError(err) return } if reqBody.IsLdap { luser, err := ctx.adm.ldap.Login(reqBody.Username, reqBody.Passwd) if err != nil { ctx.respAuthFailed(err) return } user = *luser } else { if !user.Verify(reqBody.Username, reqBody.Passwd) { ctx.respAuthFailed(errors.New("帐号或密码不正确")) return } } customerClaims.ExpiresAt = cfg.Jwt.Expires + time.Now().Unix() customerClaims.Username = reqBody.Username customerClaims.UserID = user.ID customerClaims.Mail = user.Mail customerClaims.GroupID = user.GroupID customerClaims.Root = user.Root customerClaims.Version = user.Version if reqBody.Remember { customerClaims.ExpiresAt = time.Now().Add(24 * 30 * time.Hour).Unix() } token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, customerClaims).SignedString([]byte(cfg.Jwt.SigningKey)) if err != nil { ctx.respAuthFailed(errors.New("无法生成访问凭证")) return } ctx.respSucc("", map[string]interface{}{ "token": token, "groupID": user.GroupID, "root": user.Root, "mail": user.Mail, "username": user.Username, "userID": user.ID, }) } func GetActivityList(ctx *myctx) { var ( err error reqBody ReadMoreReqParams events []models.Event isSuper bool model = models.DB() ) if err = ctx.Valid(&reqBody); err != nil { ctx.respParamError(err) return } if ctx.isSuper() { isSuper = true } if reqBody.LastID == 0 { if !isSuper { model = model.Where("group_id=?", ctx.claims.GroupID) } } else { if !isSuper { model = model.Where("group_id=? and id