Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions TODOLIST.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# TODO LIST

- [ ] 用户组权限
- [ ] 默认用户组
- [x] 用户组
- [ ] 权限
- [x] 默认用户组
- [ ] 上传到指定Categroy
- [ ] 整顿collectin info的goods 列表
- [ ] Token 使用日志显示
- [ ] Good 下载次数统计
- [ ] 接入统计
- [ ] 配置文件写入初始化用户账号密码
- [ ] 接入AuthBox
- [ ] 缓存整理
- [ ] Redis 接入
- [x] 支持Session
- [x] 支持Session
- [ ] 缓存整理
20 changes: 20 additions & 0 deletions src/models/System.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { model, SchemaDefinition, Model as M } from "mongoose";
import { Base, IDoc, IDocRaw } from "./common";

const Definition: SchemaDefinition = {
key: { type: String, required: true },
value: { type: String, required: true }
};

export interface ISystem extends IDocRaw {
key: string;
value: string;
}

const SystemSchema = new Base(Definition).createSchema();

export const Flag = "sys";

export type SystemDoc = IDoc<ISystem>;

export const Model: M<SystemDoc> = model(Flag, SystemSchema);
50 changes: 50 additions & 0 deletions src/models/User-Usergroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { model, SchemaDefinition, Model as M, SchemaTypes } from "mongoose";
import { FLAG as UF, IUser } from "@models/User";
import { FLAG as GF, IUsergroups } from "@models/Usergroup";
import { ObjectId } from "@models/common";
import { Base, IDoc, IDocRaw } from "./common";

const Definition: SchemaDefinition = {
user: {
type: SchemaTypes.ObjectId,
ref: UF,
index: true,
required: true
},
usergroup: {
type: SchemaTypes.ObjectId,
ref: GF,
index: true,
required: true
}
};

/**
* User-Usergroup Model FLAG
*/
export const FLAG = "user-usergroups";

/**
* User-Usergroup Doc Interface
*/
export interface IUserUsergroups extends IDocRaw {
user: ObjectId | IUser;
usergroup: ObjectId | IUsergroups;
}

/**
* User-Usergroup Raw Doc Interface
*/
export interface IUserUsergroupsRaw extends IUserUsergroups {
user: IUser;
usergroup: IUsergroups;
}

export type UserUsergroupDoc = IDoc<IUserUsergroups>;

const UserUsergroupsSchema = new Base(Definition).createSchema();

/**
* User-Usergroup Model
*/
export const Model: M<UserUsergroupDoc> = model(FLAG, UserUsergroupsSchema);
36 changes: 36 additions & 0 deletions src/models/Usergroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { model, SchemaDefinition, Model as M, SchemaTypes } from "mongoose";
import { FLAG as UF, IUser } from "@models/User";
import { ObjectId } from "@models/common";
import { Base, IDoc, IDocRaw } from "./common";

const Definition: SchemaDefinition = {
name: { type: String, required: true }
};

export const FLAG = "usergroups";

export interface IUsergroups extends IDocRaw {
name: string;
}

export type UsergroupDoc = IDoc<IUsergroups>;

const UsergroupsSchema = new Base(Definition).createSchema();

UsergroupsSchema.path("name").validate({
isAsync: true,
validator: async function nameExistValidator(val, respond) {
if (!this.isNew) {
const id = this.getQuery()._id;
const ug = await Model.findById(id).exec();
if (ug.toObject().user === val) {
return respond(true);
}
}
const ug = await Model.findOne({ name: val }).exec();
return respond(!ug);
},
message: "The Name is exist"
});

export const Model: M<UsergroupDoc> = model(FLAG, UsergroupsSchema);
6 changes: 6 additions & 0 deletions src/modules/common/dtos/ids.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export class GidDto implements IGidDto {
public readonly gid: ObjectId;
}

export class UGidDto implements IGidDto {
@ApiModelProperty({ type: String, description: "Usergroup ID" })
@IsMongoId()
public readonly gid: ObjectId;
}

export interface IUidDto {
/**
* User MongoID
Expand Down
38 changes: 38 additions & 0 deletions src/modules/common/services/system.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Component } from "@nestjs/common";
import { ObjectId } from "@models/common";
import { Model as SystemModel } from "@models/System";
import { Model as UsergroupsModel } from "@models/Usergroup";

@Component()
export class SystemService {

public static DEFAULT_USERGROUP_FLAG = "DEFAULT_USERGROUP";

/**
* Get Default Usergroup ID
* @returns Usergroup ID
*/
public async getDefaultUsergroup(): Promise<ObjectId> {
let gid: any = await SystemModel.findOne({
key: SystemService.DEFAULT_USERGROUP_FLAG
}).exec();
if (!gid) {
gid = (await UsergroupsModel.findOne().exec())._id;
this.setDefaultUsergroup(gid);
}
return gid;
}

/**
* Set Default Usergroup ID
* @param gid Usergroup ID
*/
public setDefaultUsergroup(gid: ObjectId) {
return SystemModel.findOneAndUpdate(
{ key: SystemService.DEFAULT_USERGROUP_FLAG }, { value: gid },
{ upsert: true }
)
.exec();
}

}
120 changes: 120 additions & 0 deletions src/modules/common/services/usergroups.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { Component, BadRequestException } from "@nestjs/common";
import { Model as UsersModel } from "@models/User";
import { Model as UsergroupsModel } from "@models/Usergroup";
import { Model as UserUsergroupsModel } from "@models/User-Usergroup";
import { ObjectId } from "@models/common";
import { DEF_PER_COUNT, IPerPage } from "@dtos/page";

@Component()
export class UsergroupsService {

private DEF_PER_OBJ: IPerPage = {
perNum: DEF_PER_COUNT,
page: 1
};

public async add(obj: object) {
try {
return await UsergroupsModel.create(obj);
} catch (error) {
throw new BadRequestException(error.toString());
}
}

public async edit(id: ObjectId, obj: object) {
try {
return await UsergroupsModel.update(
{ _id: id }, obj, { runValidators: true, context: "query" }
).exec();
} catch (error) {
throw new BadRequestException(error.toString());
}
}

public usersCount(gid: ObjectId) {
return UserUsergroupsModel.count({ usergroup: gid }).exec();
}

public async usersCountPage(id: ObjectId, perNum = DEF_PER_COUNT) {
const total = await this.usersCount(id);
return Math.ceil(total / perNum);
}

public getGroup(gid: ObjectId) {
return UsergroupsModel.findById(gid).exec();
}

public async getGroupUsers(
gid: ObjectId, pageObj: IPerPage = this.DEF_PER_OBJ
) {
const perNum = pageObj.perNum;
const page = pageObj.page;
return (await UserUsergroupsModel.find({ usergroup: gid })
.skip((page - 1) * perNum).limit(perNum)
.populate("user").exec()
).map((item) => {
return item.toObject().user;
});
}

public count() {
return UsergroupsModel.count({ }).exec();
}

public async countPage(perNum = DEF_PER_COUNT) {
const total = await this.count();
return Math.ceil(total / perNum);
}

public list(pageObj: IPerPage = this.DEF_PER_OBJ) {
const perNum = pageObj.perNum;
const page = pageObj.page;
return UsergroupsModel.find({ })
.skip((page - 1) * perNum).limit(perNum)
.sort({ createdAt: -1 })
.exec();
}

public async remove(gid: ObjectId) {
if ((await this.count()) === 1) {
throw new BadRequestException("Nnn delete unique group");
}
try {
return await UsergroupsModel.findByIdAndRemove(gid).exec();
} catch (error) {
throw new BadRequestException(error.toString());
}
}

public async addUserToGroup(gid: ObjectId, uid: ObjectId) {
if (!(await UsersModel.findById(uid).exec())) {
throw new BadRequestException("The User ID is not exist");
}
if (!(await UsergroupsModel.findById(gid).exec())) {
throw new BadRequestException("The Usergroup ID is not exist");
}
if (await UserUsergroupsModel.findOne({
user: uid, usergroup: gid
}).exec()) {
throw new BadRequestException("User has been in the usergroup");
}
try {
return await UserUsergroupsModel.create({
user: uid, usergroup: gid
});
} catch (error) {
throw new BadRequestException(error.toString());
}
}

public async removeUserFromGroup(gid: ObjectId, uid: ObjectId) {
try {
await UserUsergroupsModel.findOneAndRemove({
user: uid, usergroup: gid
}).exec();
} catch (error) {
throw new BadRequestException(error.toString());
}
}

}
58 changes: 58 additions & 0 deletions src/modules/common/services/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
import { Component, BadRequestException } from "@nestjs/common";
import { ObjectId } from "@models/common";
import { Model as UsersModel, UserDoc } from "@models/User";
import { Model as UserUsergroupsModel } from "@models/User-Usergroup";
import { IUsergroups } from "@models/Usergroup";
import { SystemService } from "@services/system";
import { IPerPage, DEF_PER_COUNT } from "@dtos/page";

@Component()
export class UsersService {

private DEF_PER_OBJ: IPerPage = {
perNum: DEF_PER_COUNT,
page: 1
};

constructor(private readonly sysSvr: SystemService) { }

public async addUser(obj, gid?: ObjectId) {
try {
const user = await UsersModel.addUser(obj.username, obj.password);
if (!gid) {
gid = await this.sysSvr.getDefaultUsergroup();
}
await UserUsergroupsModel.create({
user: user._id, usergroup: gid
});
return user;
} catch (error) {
throw new BadRequestException(error.toString());
}
}

public async removeUser(uid: ObjectId) {
try {
await UsersModel.removeUser(uid);
await UserUsergroupsModel.remove({ user: uid }).exec();
} catch (error) {
throw new BadRequestException(error.toString());
}
}

public countUsergroups(uid: ObjectId) {
return UserUsergroupsModel.count({ user: uid }).exec();
}

public async countPageUsergroups(uid: ObjectId, perNum = DEF_PER_COUNT) {
const total = await this.countUsergroups(uid);
return Math.ceil(total / perNum);
}

public async getUsergroups(
uid: ObjectId, pageObj: IPerPage = this.DEF_PER_OBJ
) {
const perNum = pageObj.perNum;
const page = pageObj.page;
const groups = await UserUsergroupsModel
.find({ user: uid }).populate("usergroup")
.skip((page - 1) * perNum).limit(perNum)
.exec();
return groups.map((item) => {
return item.toObject().usergroup as IUsergroups;
});
}

/**
* 修改`User`属性, 除了`username`
* @param id User ID
Expand Down
Loading