| const mongoose = require("mongoose"); |
| const bcrypt = require('bcryptjs'); |
|
|
| const adminSchema = mongoose.Schema({ |
| username: { |
| type: String, |
| required: true, |
| unique: true, |
| trim: true, |
| minlength: 3, |
| maxlength: 30 |
| }, |
| email: { |
| type: String, |
| required: true, |
| unique: true, |
| lowercase: true, |
| trim: true |
| }, |
| password: { |
| type: String, |
| required: true, |
| minlength: 8 |
| }, |
| firstName: { |
| type: String, |
| required: true, |
| trim: true |
| }, |
| lastName: { |
| type: String, |
| required: true, |
| trim: true |
| }, |
| role: { |
| type: String, |
| enum: ['super_admin', 'admin', 'moderator', 'support'], |
| default: 'admin' |
| }, |
| permissions: { |
| users: { |
| view: { type: Boolean, default: true }, |
| create: { type: Boolean, default: false }, |
| edit: { type: Boolean, default: false }, |
| delete: { type: Boolean, default: false } |
| }, |
| products: { |
| view: { type: Boolean, default: true }, |
| create: { type: Boolean, default: true }, |
| edit: { type: Boolean, default: true }, |
| delete: { type: Boolean, default: false } |
| }, |
| orders: { |
| view: { type: Boolean, default: true }, |
| edit: { type: Boolean, default: true }, |
| delete: { type: Boolean, default: false } |
| }, |
| analytics: { |
| view: { type: Boolean, default: true }, |
| export: { type: Boolean, default: false } |
| }, |
| settings: { |
| view: { type: Boolean, default: false }, |
| edit: { type: Boolean, default: false } |
| } |
| }, |
| avatar: { |
| type: String, |
| default: null |
| }, |
| isActive: { |
| type: Boolean, |
| default: true |
| }, |
| lastLogin: { |
| type: Date, |
| default: null |
| }, |
| twoFactorEnabled: { |
| type: Boolean, |
| default: false |
| }, |
| twoFactorSecret: { |
| type: String, |
| default: null |
| }, |
| preferences: { |
| theme: { type: String, default: 'dark' }, |
| language: { type: String, default: 'en' }, |
| notifications: { |
| email: { type: Boolean, default: true }, |
| push: { type: Boolean, default: true }, |
| sms: { type: Boolean, default: false } |
| } |
| }, |
| activityLog: [{ |
| action: String, |
| description: String, |
| ipAddress: String, |
| userAgent: String, |
| timestamp: { type: Date, default: Date.now } |
| }] |
| }, { |
| timestamps: true |
| }); |
|
|
| adminSchema.pre('save', async function(next) { |
| if (!this.isModified('password')) { |
| return next(); |
| } |
| |
| try { |
| const salt = await bcrypt.genSalt(12); |
| this.password = await bcrypt.hash(this.password, salt); |
| next(); |
| } catch (error) { |
| next(error); |
| } |
| }); |
|
|
| adminSchema.methods.checkPassword = async function(password) { |
| return await bcrypt.compare(password, this.password); |
| }; |
|
|
| adminSchema.methods.hasPermission = function(resource, action) { |
| if (this.role === 'super_admin') return true; |
| return this.permissions[resource]?.[action] || false; |
| }; |
|
|
| adminSchema.methods.getFullName = function() { |
| return `${this.firstName} ${this.lastName}`; |
| }; |
|
|
| module.exports = mongoose.model('Admin', adminSchema); |