UNPKG

recoder-code

Version:

Complete AI-powered development platform with ML model training, plugin registry, real-time collaboration, monitoring, infrastructure automation, and enterprise deployment capabilities

376 lines 13.8 kB
"use strict"; /** * User Entity * Represents users who can publish and manage packages */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.User = exports.UserStatus = exports.UserRole = void 0; const typeorm_1 = require("typeorm"); const Package_1 = require("./Package"); const Download_1 = require("./Download"); const ApiKey_1 = require("./ApiKey"); var UserRole; (function (UserRole) { UserRole["USER"] = "user"; UserRole["MAINTAINER"] = "maintainer"; UserRole["ADMIN"] = "admin"; UserRole["MODERATOR"] = "moderator"; })(UserRole = exports.UserRole || (exports.UserRole = {})); var UserStatus; (function (UserStatus) { UserStatus["ACTIVE"] = "active"; UserStatus["SUSPENDED"] = "suspended"; UserStatus["PENDING"] = "pending"; UserStatus["BANNED"] = "banned"; })(UserStatus = exports.UserStatus || (exports.UserStatus = {})); let User = class User { // Virtual properties get is_admin() { return this.role === UserRole.ADMIN; } get is_moderator() { return this.role === UserRole.MODERATOR || this.is_admin; } get is_maintainer() { return this.role === UserRole.MAINTAINER || this.is_moderator; } get can_publish() { return this.permissions?.can_publish !== false && this.status === UserStatus.ACTIVE; } get can_manage() { return this.permissions?.can_manage_teams !== false && this.is_maintainer; } get display_name() { return this.full_name || this.username; } get npm_display_name() { return this.npm_username || this.username; } // Methods recordLogin(ip) { this.last_login = new Date(); this.login_count += 1; if (ip) { this.last_login_ip = ip; } } suspend(reason, until) { this.status = UserStatus.SUSPENDED; this.suspension_reason = reason; this.suspended_until = until; } unsuspend() { this.status = UserStatus.ACTIVE; this.suspension_reason = undefined; this.suspended_until = undefined; } ban(reason) { this.status = UserStatus.BANNED; this.suspension_reason = reason; this.is_active = false; } activate() { this.status = UserStatus.ACTIVE; this.is_active = true; this.suspension_reason = undefined; this.suspended_until = undefined; } verifyEmail() { this.email_verified = true; this.verification_token = undefined; this.verification_expires = undefined; if (this.status === UserStatus.PENDING) { this.status = UserStatus.ACTIVE; } } generateVerificationToken() { const token = require('crypto').randomBytes(32).toString('hex'); this.verification_token = token; this.verification_expires = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours return token; } generateResetToken() { const token = require('crypto').randomBytes(32).toString('hex'); this.reset_token = token; this.reset_expires = new Date(Date.now() + 60 * 60 * 1000); // 1 hour return token; } clearResetToken() { this.reset_token = undefined; this.reset_expires = undefined; } updateStats(statsUpdate) { if (!statsUpdate) return; const currentStats = this.stats || { packages_published: 0, total_downloads: 0, followers: 0, following: 0, packages_maintained: 0 }; this.stats = { packages_published: statsUpdate.packages_published ?? currentStats.packages_published, total_downloads: statsUpdate.total_downloads ?? currentStats.total_downloads, followers: statsUpdate.followers ?? currentStats.followers, following: statsUpdate.following ?? currentStats.following, packages_maintained: statsUpdate.packages_maintained ?? currentStats.packages_maintained }; } addOrganization(org) { if (!this.organizations) { this.organizations = []; } if (!this.organizations.includes(org)) { this.organizations.push(org); } } removeOrganization(org) { if (this.organizations) { this.organizations = this.organizations.filter(o => o !== org); } } hasPermission(permission) { if (this.is_admin) return true; return this.permissions?.[permission] === true; } canAccessPackage(pkg) { if (this.is_admin) return true; if (pkg.owner_id === this.id) return true; if (pkg.organization && this.organizations?.includes(pkg.organization)) return true; return false; } canModifyPackage(pkg) { if (this.is_admin) return true; if (pkg.owner_id === this.id) return true; // Check if user is maintainer of the package if (pkg.maintainers) { const maintainer = pkg.maintainers.find(m => m.name === this.username || m.email === this.email); if (maintainer) return true; } return false; } getRateLimits() { if (this.is_admin) { return { requests: 10000, window: 3600 }; // 10k/hour } if (this.is_maintainer) { return { requests: 5000, window: 3600 }; // 5k/hour } return { requests: 1000, window: 3600 }; // 1k/hour } toNpmFormat() { return { _id: `org.couchdb.user:${this.npm_username || this.username}`, name: this.npm_username || this.username, email: this.npm_email || this.email, type: 'user', roles: [], date: this.created_at.toISOString() }; } toPublicFormat() { return { id: this.id, username: this.username, full_name: this.full_name, bio: this.bio, avatar_url: this.avatar_url, website: this.website, location: this.location, github_username: this.github_username, twitter_username: this.twitter_username, created_at: this.created_at, stats: this.stats, organizations: this.organizations || [] }; } toPrivateFormat() { return { ...this.toPublicFormat(), email: this.email, email_verified: this.email_verified, role: this.role, status: this.status, preferences: this.preferences, permissions: this.permissions, last_login: this.last_login }; } }; __decorate([ (0, typeorm_1.PrimaryGeneratedColumn)('uuid'), __metadata("design:type", String) ], User.prototype, "id", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, unique: true }), __metadata("design:type", String) ], User.prototype, "username", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, unique: true }), __metadata("design:type", String) ], User.prototype, "email", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], User.prototype, "full_name", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'text', nullable: true }), __metadata("design:type", String) ], User.prototype, "bio", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], User.prototype, "avatar_url", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], User.prototype, "website", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, nullable: true }), __metadata("design:type", String) ], User.prototype, "location", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 50, nullable: true }), __metadata("design:type", String) ], User.prototype, "timezone", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255 }), __metadata("design:type", String) ], User.prototype, "password_hash", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, unique: true, nullable: true }), __metadata("design:type", String) ], User.prototype, "npm_username", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], User.prototype, "npm_email", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'enum', enum: UserRole, default: UserRole.USER }), __metadata("design:type", String) ], User.prototype, "role", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'enum', enum: UserStatus, default: UserStatus.ACTIVE }), __metadata("design:type", String) ], User.prototype, "status", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'boolean', default: false }), __metadata("design:type", Boolean) ], User.prototype, "email_verified", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'boolean', default: true }), __metadata("design:type", Boolean) ], User.prototype, "is_active", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], User.prototype, "github_username", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], User.prototype, "twitter_username", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'json', nullable: true }), __metadata("design:type", Object) ], User.prototype, "preferences", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'json', nullable: true }), __metadata("design:type", Object) ], User.prototype, "permissions", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'json', nullable: true }), __metadata("design:type", Object) ], User.prototype, "stats", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'json', nullable: true }), __metadata("design:type", Array) ], User.prototype, "organizations", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], User.prototype, "verification_token", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'timestamp', nullable: true }), __metadata("design:type", Date) ], User.prototype, "verification_expires", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], User.prototype, "reset_token", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'timestamp', nullable: true }), __metadata("design:type", Date) ], User.prototype, "reset_expires", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'timestamp', nullable: true }), __metadata("design:type", Date) ], User.prototype, "last_login", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 45, nullable: true }), __metadata("design:type", String) ], User.prototype, "last_login_ip", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'int', default: 0 }), __metadata("design:type", Number) ], User.prototype, "login_count", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'timestamp', nullable: true }), __metadata("design:type", Date) ], User.prototype, "suspended_until", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'text', nullable: true }), __metadata("design:type", String) ], User.prototype, "suspension_reason", void 0); __decorate([ (0, typeorm_1.CreateDateColumn)(), __metadata("design:type", Date) ], User.prototype, "created_at", void 0); __decorate([ (0, typeorm_1.UpdateDateColumn)(), __metadata("design:type", Date) ], User.prototype, "updated_at", void 0); __decorate([ (0, typeorm_1.OneToMany)(() => Package_1.Package, pkg => pkg.owner), __metadata("design:type", Array) ], User.prototype, "packages", void 0); __decorate([ (0, typeorm_1.OneToMany)(() => Download_1.Download, download => download.user), __metadata("design:type", Array) ], User.prototype, "downloads", void 0); __decorate([ (0, typeorm_1.OneToMany)(() => ApiKey_1.ApiKey, apiKey => apiKey.user, { cascade: true }), __metadata("design:type", Array) ], User.prototype, "api_keys", void 0); User = __decorate([ (0, typeorm_1.Entity)('users'), (0, typeorm_1.Index)(['username'], { unique: true }), (0, typeorm_1.Index)(['email'], { unique: true }), (0, typeorm_1.Index)(['npm_username'], { unique: true }) ], User); exports.User = User; //# sourceMappingURL=User.js.map