mirror of
https://github.com/gravitl/netmaker.git
synced 2026-04-23 00:17:10 +08:00
edda2868fc
* feat(go): add user schema; * feat(go): migrate to user schema; * feat(go): add audit fields; * feat(go): remove unused fields from the network model; * feat(go): add network schema; * feat(go): migrate to network schema; * refactor(go): add comment to clarify migration logic; * fix(go): test failures; * fix(go): test failures; * feat(go): change membership table to store memberships at all scopes; * feat(go): add schema for access grants; * feat(go): remove nameservers from new networks table; ensure db passed for schema functions; * feat(go): set max conns for sqlite to 1; * fix(go): issues updating user account status; * refactor(go): remove converters and access grants; * refactor(go): add json tags in schema models; * refactor(go): rename file to migrate_v1_6_0.go; * refactor(go): add user groups and user roles tables; use schema tables; * refactor(go): inline get and list from schema package; * refactor(go): inline get network and list users from schema package; * fix(go): staticcheck issues; * fix(go): remove test not in use; fix test case; * fix(go): validate network; * fix(go): resolve static checks; * fix(go): new models errors; * fix(go): test errors; * fix(go): handle no records; * fix(go): add validations for user object; * fix(go): set correct extclient status; * fix(go): test error; * feat(go): make schema the base package; * feat(go): add host schema; * feat(go): use schema host everywhere; * feat(go): inline get host, list hosts and delete host; * feat(go): use non-ptr value; * feat(go): use save to upsert all fields; * feat(go): use save to upsert all fields; * feat(go): save turn endpoint as string; * feat(go): check for gorm error record not found; * fix(go): test failures; * fix(go): update all network fields; * fix(go): update all network fields; * feat(go): add paginated list networks api; * feat(go): add paginated list users api; * feat(go): add paginated list hosts api; * feat(go): add pagination to list groups api; * fix(go): comment; * fix(go): implement marshal and unmarshal text for custom types; * fix(go): implement marshal and unmarshal json for custom types; * fix(go): just use the old model for unmarshalling; * fix(go): implement marshal and unmarshal json for custom types; * feat(go): remove paginated list networks api; * feat(go): use custom paginated response object; * fix(go): ensure default values for page and per_page are used when not passed; * fix(go): rename v1.6.0 to v1.5.1; * fix(go): check for gorm.ErrRecordNotFound instead of database.IsEmptyRecord; * fix(go): use host id, not pending host id; * feat(go): add filters to paginated apis; * feat(go): add filters to paginated apis; * feat(go): remove check for max username length; * feat(go): add filters to count as well; * feat(go): use library to check email address validity; * feat(go): ignore pagination if params not passed; * fix(go): pagination issues; * fix(go): check exists before using; * fix(go): remove debug log; * fix(go): use gorm err record not found; * fix(go): use gorm err record not found; * fix(go): use user principal name when creating pending user; * fix(go): use schema package for consts; * fix(go): prevent disabling superadmin user; Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): swap is admin and is superadmin; Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): remove dead code block; https://github.com/gravitl/netmaker/pull/3910#discussion_r2928837937 * fix(go): incorrect message when trying to disable self; https://github.com/gravitl/netmaker/pull/3910#discussion_r2928837934 * fix(go): use correct header; Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): return after error response; Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): use correct order of params; https://github.com/gravitl/netmaker/pull/3910#discussion_r2929593036 * fix(go): set default values for page and page size; use v2 instead of /list; * Update logic/auth.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * Update schema/user_roles.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): syntax error; * fix(go): set default values when page and per_page are not passed or 0; * fix(go): use uuid.parse instead of uuid.must parse; * fix(go): review errors; * fix(go): review errors; * Update controllers/user.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * Update controllers/user.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * NM-163: fix errors: * Update db/types/options.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): persist return user in event; * Update db/types/options.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * NM-163: duplicate lines of code * NM-163: fix(go): fix missing return and filter parsing in user controller - Add missing return after error response in updateUserAccountStatus to prevent double-response and spurious ext-client side-effects - Use switch statements in listUsers to skip unrecognized account_status and mfa_status filter values * fix(go): check for both min and max page size; * fix(go): enclose transfer superadmin in transaction; * fix(go): review errors; * fix(go): remove free tier checks; * fix(go): review fixes; --------- Co-authored-by: VishalDalwadi <dalwadivishal26@gmail.com> Co-authored-by: Vishal Dalwadi <51291657+VishalDalwadi@users.noreply.github.com> Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>
155 lines
6.2 KiB
Go
155 lines
6.2 KiB
Go
package schema
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gravitl/netmaker/db"
|
|
"gorm.io/datatypes"
|
|
)
|
|
|
|
type Action string
|
|
|
|
const (
|
|
Create Action = "CREATE"
|
|
Update Action = "UPDATE"
|
|
Delete Action = "DELETE"
|
|
DeleteAll Action = "DELETE_ALL"
|
|
Login Action = "LOGIN"
|
|
LogOut Action = "LOGOUT"
|
|
Connect Action = "CONNECT"
|
|
Sync Action = "SYNC"
|
|
RefreshKey Action = "REFRESH_KEY"
|
|
RefreshAllKeys Action = "REFRESH_ALL_KEYS"
|
|
SyncAll Action = "SYNC_ALL"
|
|
UpgradeAll Action = "UPGRADE_ALL"
|
|
Disconnect Action = "DISCONNECT"
|
|
JoinHostToNet Action = "JOIN_HOST_TO_NETWORK"
|
|
RemoveHostFromNet Action = "REMOVE_HOST_FROM_NETWORK"
|
|
EnableMFA Action = "ENABLE_MFA"
|
|
DisableMFA Action = "DISABLE_MFA"
|
|
EnforceMFA Action = "ENFORCE_MFA"
|
|
UnenforceMFA Action = "UNENFORCE_MFA"
|
|
EnableBasicAuth Action = "ENABLE_BASIC_AUTH"
|
|
DisableBasicAuth Action = "DISABLE_BASIC_AUTH"
|
|
EnableTelemetry Action = "ENABLE_TELEMETRY"
|
|
DisableTelemetry Action = "DISABLE_TELEMETRY"
|
|
UpdateClientSettings Action = "UPDATE_CLIENT_SETTINGS"
|
|
UpdateAuthenticationSecuritySettings Action = "UPDATE_AUTHENTICATION_SECURITY_SETTINGS"
|
|
UpdateMonitoringAndDebuggingSettings Action = "UPDATE_MONITORING_AND_DEBUGGING_SETTINGS"
|
|
UpdateSMTPSettings Action = "UPDATE_EMAIL_SETTINGS"
|
|
UpdateIDPSettings Action = "UPDATE_IDP_SETTINGS"
|
|
EnableFlowLogs Action = "ENABLE_FLOW_LOGS"
|
|
DisableFlowLogs Action = "DISABLE_FLOW_LOGS"
|
|
GatewayAssign Action = "GATEWAY_ASSIGN"
|
|
GatewayUnAssign Action = "GATEWAY_UNASSIGN"
|
|
)
|
|
|
|
type SubjectType string
|
|
|
|
const (
|
|
UserSub SubjectType = "USER"
|
|
UserAccessTokenSub SubjectType = "USER_ACCESS_TOKEN"
|
|
DeviceSub SubjectType = "DEVICE"
|
|
NodeSub SubjectType = "NODE"
|
|
GatewaySub SubjectType = "GATEWAY"
|
|
SettingSub SubjectType = "SETTING"
|
|
AclSub SubjectType = "ACL"
|
|
TagSub SubjectType = "TAG"
|
|
UserRoleSub SubjectType = "USER_ROLE"
|
|
UserGroupSub SubjectType = "USER_GROUP"
|
|
UserInviteSub SubjectType = "USER_INVITE"
|
|
PendingUserSub SubjectType = "PENDING_USER"
|
|
EgressSub SubjectType = "EGRESS"
|
|
NetworkSub SubjectType = "NETWORK"
|
|
DashboardSub SubjectType = "DASHBOARD"
|
|
EnrollmentKeySub SubjectType = "ENROLLMENT_KEY"
|
|
ClientAppSub SubjectType = "CLIENT-APP"
|
|
NameserverSub SubjectType = "NAMESERVER"
|
|
PostureCheckSub SubjectType = "POSTURE_CHECK"
|
|
)
|
|
|
|
func (sub SubjectType) String() string {
|
|
return string(sub)
|
|
}
|
|
|
|
type Origin string
|
|
|
|
const (
|
|
Dashboard Origin = "DASHBOARD"
|
|
Api Origin = "API"
|
|
NMCTL Origin = "NMCTL"
|
|
ClientApp Origin = "CLIENT-APP"
|
|
)
|
|
|
|
type Event struct {
|
|
ID string `gorm:"primaryKey" json:"id"`
|
|
Action Action `gorm:"action" json:"action"`
|
|
Source datatypes.JSON `gorm:"source" json:"source"`
|
|
Origin Origin `gorm:"origin" json:"origin"`
|
|
Target datatypes.JSON `gorm:"target" json:"target"`
|
|
NetworkID NetworkID `gorm:"network_id" json:"network_id"`
|
|
TriggeredBy string `gorm:"triggered_by" json:"triggered_by"`
|
|
Diff datatypes.JSON `gorm:"diff" json:"diff"`
|
|
TimeStamp time.Time `gorm:"time_stamp" json:"time_stamp"`
|
|
}
|
|
|
|
func (a *Event) Get(ctx context.Context) error {
|
|
return db.FromContext(ctx).Model(&Event{}).First(&a).Where("id = ?", a.ID).Error
|
|
}
|
|
|
|
func (a *Event) Update(ctx context.Context) error {
|
|
return db.FromContext(ctx).Model(&Event{}).Where("id = ?", a.ID).Updates(&a).Error
|
|
}
|
|
|
|
func (a *Event) Create(ctx context.Context) error {
|
|
return db.FromContext(ctx).Model(&Event{}).Create(&a).Error
|
|
}
|
|
|
|
func (a *Event) ListByNetwork(ctx context.Context, from, to time.Time) (ats []Event, err error) {
|
|
if !from.IsZero() && !to.IsZero() {
|
|
// "created_at BETWEEN ? AND ?
|
|
err = db.FromContext(ctx).Model(&Event{}).Where("network_id = ? AND time_stamp BETWEEN ? AND ?",
|
|
a.NetworkID, from, to).Order("time_stamp DESC").Find(&ats).Error
|
|
return
|
|
}
|
|
err = db.FromContext(ctx).Model(&Event{}).Where("network_id = ?", a.NetworkID).Order("time_stamp DESC").Find(&ats).Error
|
|
|
|
return
|
|
}
|
|
|
|
func (a *Event) ListByUser(ctx context.Context, from, to time.Time) (ats []Event, err error) {
|
|
if !from.IsZero() && !to.IsZero() {
|
|
err = db.FromContext(ctx).Model(&Event{}).Where("triggered_by = ? AND time_stamp BETWEEN ? AND ?",
|
|
a.TriggeredBy, from, to).Order("time_stamp DESC").Find(&ats).Error
|
|
return
|
|
}
|
|
err = db.FromContext(ctx).Model(&Event{}).Where("triggered_by = ?", a.TriggeredBy).Order("time_stamp DESC").Find(&ats).Error
|
|
return
|
|
}
|
|
|
|
func (a *Event) ListByUserAndNetwork(ctx context.Context, from, to time.Time) (ats []Event, err error) {
|
|
if !from.IsZero() && !to.IsZero() {
|
|
err = db.FromContext(ctx).Model(&Event{}).Where("network_id = ? AND triggered_by = ? AND time_stamp BETWEEN ? AND ?",
|
|
a.NetworkID, a.TriggeredBy, from, to).Order("time_stamp DESC").Find(&ats).Error
|
|
return
|
|
}
|
|
err = db.FromContext(ctx).Model(&Event{}).Where("network_id = ? AND triggered_by = ?",
|
|
a.NetworkID, a.TriggeredBy).Order("time_stamp DESC").Find(&ats).Error
|
|
return
|
|
}
|
|
|
|
func (a *Event) List(ctx context.Context, from, to time.Time) (ats []Event, err error) {
|
|
if !from.IsZero() && !to.IsZero() {
|
|
err = db.FromContext(ctx).Model(&Event{}).Where("time_stamp BETWEEN ? AND ?", from, to).Order("time_stamp DESC").Find(&ats).Error
|
|
return
|
|
}
|
|
err = db.FromContext(ctx).Model(&Event{}).Order("time_stamp DESC").Find(&ats).Error
|
|
return
|
|
}
|
|
|
|
func (a *Event) DeleteOldEvents(ctx context.Context, retentionDays int) error {
|
|
cutoff := time.Now().AddDate(0, 0, -retentionDays)
|
|
return db.FromContext(ctx).Model(&Event{}).Where("created_at < ?", cutoff).Delete(&Event{}).Error
|
|
}
|