feature: verify tokens

This commit is contained in:
2024-11-17 22:28:29 +05:30
parent 26a00c9f7c
commit 9d40c9d7ec
57 changed files with 4188 additions and 276 deletions

View File

@@ -21,6 +21,7 @@ import (
"gitserver.in/patialtech/rano/db/ent/todo"
"gitserver.in/patialtech/rano/db/ent/user"
"gitserver.in/patialtech/rano/db/ent/usersession"
"gitserver.in/patialtech/rano/db/ent/verifytoken"
)
// Client is the client that holds all ent builders.
@@ -40,6 +41,8 @@ type Client struct {
User *UserClient
// UserSession is the client for interacting with the UserSession builders.
UserSession *UserSessionClient
// VerifyToken is the client for interacting with the VerifyToken builders.
VerifyToken *VerifyTokenClient
}
// NewClient creates a new client configured with the given options.
@@ -57,6 +60,7 @@ func (c *Client) init() {
c.Todo = NewTodoClient(c.config)
c.User = NewUserClient(c.config)
c.UserSession = NewUserSessionClient(c.config)
c.VerifyToken = NewVerifyTokenClient(c.config)
}
type (
@@ -155,6 +159,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) {
Todo: NewTodoClient(cfg),
User: NewUserClient(cfg),
UserSession: NewUserSessionClient(cfg),
VerifyToken: NewVerifyTokenClient(cfg),
}, nil
}
@@ -180,6 +185,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
Todo: NewTodoClient(cfg),
User: NewUserClient(cfg),
UserSession: NewUserSessionClient(cfg),
VerifyToken: NewVerifyTokenClient(cfg),
}, nil
}
@@ -209,7 +215,7 @@ func (c *Client) Close() error {
// In order to add hooks to a specific client, call: `client.Node.Use(...)`.
func (c *Client) Use(hooks ...Hook) {
for _, n := range []interface{ Use(...Hook) }{
c.AccessControl, c.Audit, c.Role, c.Todo, c.User, c.UserSession,
c.AccessControl, c.Audit, c.Role, c.Todo, c.User, c.UserSession, c.VerifyToken,
} {
n.Use(hooks...)
}
@@ -219,7 +225,7 @@ func (c *Client) Use(hooks ...Hook) {
// In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`.
func (c *Client) Intercept(interceptors ...Interceptor) {
for _, n := range []interface{ Intercept(...Interceptor) }{
c.AccessControl, c.Audit, c.Role, c.Todo, c.User, c.UserSession,
c.AccessControl, c.Audit, c.Role, c.Todo, c.User, c.UserSession, c.VerifyToken,
} {
n.Intercept(interceptors...)
}
@@ -240,6 +246,8 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
return c.User.mutate(ctx, m)
case *UserSessionMutation:
return c.UserSession.mutate(ctx, m)
case *VerifyTokenMutation:
return c.VerifyToken.mutate(ctx, m)
default:
return nil, fmt.Errorf("ent: unknown mutation type %T", m)
}
@@ -933,6 +941,22 @@ func (c *UserClient) QueryAuditLogs(u *User) *AuditQuery {
return query
}
// QueryVerifyTokens queries the verify_tokens edge of a User.
func (c *UserClient) QueryVerifyTokens(u *User) *VerifyTokenQuery {
query := (&VerifyTokenClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := u.ID
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, id),
sqlgraph.To(verifytoken.Table, verifytoken.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.VerifyTokensTable, user.VerifyTokensColumn),
)
fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
return fromV, nil
}
return query
}
// Hooks returns the client hooks.
func (c *UserClient) Hooks() []Hook {
return c.hooks.User
@@ -1107,12 +1131,162 @@ func (c *UserSessionClient) mutate(ctx context.Context, m *UserSessionMutation)
}
}
// VerifyTokenClient is a client for the VerifyToken schema.
type VerifyTokenClient struct {
config
}
// NewVerifyTokenClient returns a client for the VerifyToken from the given config.
func NewVerifyTokenClient(c config) *VerifyTokenClient {
return &VerifyTokenClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `verifytoken.Hooks(f(g(h())))`.
func (c *VerifyTokenClient) Use(hooks ...Hook) {
c.hooks.VerifyToken = append(c.hooks.VerifyToken, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `verifytoken.Intercept(f(g(h())))`.
func (c *VerifyTokenClient) Intercept(interceptors ...Interceptor) {
c.inters.VerifyToken = append(c.inters.VerifyToken, interceptors...)
}
// Create returns a builder for creating a VerifyToken entity.
func (c *VerifyTokenClient) Create() *VerifyTokenCreate {
mutation := newVerifyTokenMutation(c.config, OpCreate)
return &VerifyTokenCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of VerifyToken entities.
func (c *VerifyTokenClient) CreateBulk(builders ...*VerifyTokenCreate) *VerifyTokenCreateBulk {
return &VerifyTokenCreateBulk{config: c.config, builders: builders}
}
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
// a builder and applies setFunc on it.
func (c *VerifyTokenClient) MapCreateBulk(slice any, setFunc func(*VerifyTokenCreate, int)) *VerifyTokenCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &VerifyTokenCreateBulk{err: fmt.Errorf("calling to VerifyTokenClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*VerifyTokenCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &VerifyTokenCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for VerifyToken.
func (c *VerifyTokenClient) Update() *VerifyTokenUpdate {
mutation := newVerifyTokenMutation(c.config, OpUpdate)
return &VerifyTokenUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *VerifyTokenClient) UpdateOne(vt *VerifyToken) *VerifyTokenUpdateOne {
mutation := newVerifyTokenMutation(c.config, OpUpdateOne, withVerifyToken(vt))
return &VerifyTokenUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *VerifyTokenClient) UpdateOneID(id int64) *VerifyTokenUpdateOne {
mutation := newVerifyTokenMutation(c.config, OpUpdateOne, withVerifyTokenID(id))
return &VerifyTokenUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for VerifyToken.
func (c *VerifyTokenClient) Delete() *VerifyTokenDelete {
mutation := newVerifyTokenMutation(c.config, OpDelete)
return &VerifyTokenDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *VerifyTokenClient) DeleteOne(vt *VerifyToken) *VerifyTokenDeleteOne {
return c.DeleteOneID(vt.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *VerifyTokenClient) DeleteOneID(id int64) *VerifyTokenDeleteOne {
builder := c.Delete().Where(verifytoken.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &VerifyTokenDeleteOne{builder}
}
// Query returns a query builder for VerifyToken.
func (c *VerifyTokenClient) Query() *VerifyTokenQuery {
return &VerifyTokenQuery{
config: c.config,
ctx: &QueryContext{Type: TypeVerifyToken},
inters: c.Interceptors(),
}
}
// Get returns a VerifyToken entity by its id.
func (c *VerifyTokenClient) Get(ctx context.Context, id int64) (*VerifyToken, error) {
return c.Query().Where(verifytoken.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *VerifyTokenClient) GetX(ctx context.Context, id int64) *VerifyToken {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// QueryUser queries the user edge of a VerifyToken.
func (c *VerifyTokenClient) QueryUser(vt *VerifyToken) *UserQuery {
query := (&UserClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := vt.ID
step := sqlgraph.NewStep(
sqlgraph.From(verifytoken.Table, verifytoken.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, verifytoken.UserTable, verifytoken.UserColumn),
)
fromV = sqlgraph.Neighbors(vt.driver.Dialect(), step)
return fromV, nil
}
return query
}
// Hooks returns the client hooks.
func (c *VerifyTokenClient) Hooks() []Hook {
return c.hooks.VerifyToken
}
// Interceptors returns the client interceptors.
func (c *VerifyTokenClient) Interceptors() []Interceptor {
return c.inters.VerifyToken
}
func (c *VerifyTokenClient) mutate(ctx context.Context, m *VerifyTokenMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&VerifyTokenCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&VerifyTokenUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&VerifyTokenUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&VerifyTokenDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown VerifyToken mutation op: %q", m.Op())
}
}
// hooks and interceptors per client, for fast access.
type (
hooks struct {
AccessControl, Audit, Role, Todo, User, UserSession []ent.Hook
AccessControl, Audit, Role, Todo, User, UserSession, VerifyToken []ent.Hook
}
inters struct {
AccessControl, Audit, Role, Todo, User, UserSession []ent.Interceptor
AccessControl, Audit, Role, Todo, User, UserSession,
VerifyToken []ent.Interceptor
}
)