Skip to content

Commit

Permalink
feat: user add avatar change
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidZORO committed Jun 2, 2020
1 parent 5925c5a commit c317528
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 380 deletions.
3 changes: 3 additions & 0 deletions packages/_leaa-common/src/dtos/user/user.create-one.req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class UserCreateOneReq {
@Length(1, 64)
name?: string;

@IsOptional()
is_superuser?: number;

@IsNotEmpty()
@Length(6, 64)
password!: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/_leaa-common/src/dtos/user/user.update-one.req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export class UserUpdateOneReq {
@IsOptional()
last_token_at?: Date;

@IsOptional()
is_superuser?: number;

// @IsOptional()
//
// roleSlugs?: string[];
Expand Down
6 changes: 3 additions & 3 deletions packages/_leaa-common/src/entrys/attachment.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Entity, Column } from 'typeorm';
import { Expose } from 'class-transformer';

import { Base } from '@leaa/common/src/entrys';
import { buildUrl, buildUrlAt2x } from '@leaa/api/src/utils/attachment.util';
import { genUrl, genUrlAt2x } from '@leaa/api/src/utils/attachment.util';

@Entity('attachments')
export class Attachment extends Base {
Expand Down Expand Up @@ -82,11 +82,11 @@ export class Attachment extends Base {

@Expose()
get url(): null | undefined | string {
return buildUrl(this);
return genUrl(this);
}

@Expose()
get urlAt2x(): null | undefined | string {
return buildUrlAt2x(this);
return genUrlAt2x(this);
}
}
18 changes: 17 additions & 1 deletion packages/_leaa-common/src/entrys/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Index, Entity, Column, JoinTable, ManyToMany } from 'typeorm';
import { Index, Entity, Column, JoinTable, ManyToMany, AfterLoad, BeforeInsert } from 'typeorm';
import { Exclude } from 'class-transformer';

import { Base, Role, Permission, Address, Attachment } from '@leaa/common/src/entrys';
import { transAvatarUrl, genAvatarUrl } from '@leaa/api/src/utils/attachment.util';

@Entity('users')
// @Index('users_phone_unique', ['phone'], { unique: true })
Expand All @@ -20,6 +21,17 @@ export class User extends Base {
@Column({ type: 'varchar', nullable: true, default: null })
avatar_url?: string | null;

@AfterLoad()
AfterLoad() {
this.avatar_url = transAvatarUrl(this.avatar_url);
}

@BeforeInsert()
async BeforeInsert() {
// set default avatar
this.avatar_url = genAvatarUrl(this.email);
}

@Column({ type: 'int', default: 0 })
status?: number;

Expand All @@ -30,6 +42,10 @@ export class User extends Base {
@Column({ type: 'int', default: 0 })
is_admin?: number;

@Exclude({ toPlainOnly: true })
@Column({ type: 'int', default: 0, select: false })
is_superuser?: number;

@Exclude()
@Column({ type: 'varchar', length: 32, nullable: true, select: false })
last_login_ip?: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/leaa-api/src/configs/attachment.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const URL_PREFIX_BY_LOCAL = `${envConfig.SERVER_PROTOCOL}://${envConfig.SERVER_H
: '' // prod not PORT, e.g. http://test-leaa.com/attachments/upload
}`;
const URL_PREFIX_BY_OSS = `${envConfig.SERVER_PROTOCOL}://${envConfig.ATTACHMENT_OSS_ALIYUN_BUCKET}.${envConfig.ATTACHMENT_OSS_ALIYUN_REGION}.aliyuncs.com`; // eslint-disable-line max-len
const URL_PREFIX_BY_AUTO = envConfig.ATTACHMENT_SAVE_IN_OSS ? URL_PREFIX_BY_OSS : URL_PREFIX_BY_LOCAL;

const UPLOAD_ENDPOINT_BY_LOCAL = `${URL_PREFIX_BY_LOCAL}/v1/attachments/upload`;
const UPLOAD_ENDPOINT_BY_OSS = URL_PREFIX_BY_OSS; // eslint-disable-line max-len
Expand All @@ -28,6 +29,7 @@ export const attachmentConfig = {
ALLOW_FILE_TYPES,
URL_PREFIX_BY_LOCAL,
URL_PREFIX_BY_OSS,
URL_PREFIX_BY_AUTO,
UPLOAD_ENDPOINT_BY_LOCAL,
UPLOAD_ENDPOINT_BY_OSS,
};
1 change: 1 addition & 0 deletions packages/leaa-api/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './permissions.decorator';
export * from './language.decorator';
export * from './jwt-user.decorator';
10 changes: 10 additions & 0 deletions packages/leaa-api/src/decorators/jwt-user.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { User } from '@leaa/common/src/entrys';

export const JwtUser = createParamDecorator(
(data: unknown, executionCtx: ExecutionContext): User => {
const [req] = executionCtx.getArgs();

return req.user;
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@leaa/common/src/interfaces';
import { ConfigService } from '@leaa/api/src/modules/v1/config/config.service';
import { Attachment } from '@leaa/common/src/entrys';
import { logger, isAt2x, uuid, buildUrl, buildUrlAt2x, getAt2xPath } from '@leaa/api/src/utils';
import { logger, isAt2x, uuid, genUrl, genUrlAt2x, getAt2xPath } from '@leaa/api/src/utils';
import { attachmentConfig } from '@leaa/api/src/configs';
import { isUUID } from '@nestjs/common/utils/is-uuid';

Expand Down Expand Up @@ -112,8 +112,8 @@ export class SaveInLocalService {
// eslint-disable-next-line consistent-return
return this.attachmentRepo.save({
...attachmentData,
url: buildUrl(attachmentData as Attachment),
urlAt2x: buildUrlAt2x(attachmentData as Attachment),
url: genUrl(attachmentData as Attachment),
urlAt2x: genUrlAt2x(attachmentData as Attachment),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { Attachment } from '@leaa/common/src/entrys';
import { attachmentConfig } from '@leaa/api/src/configs';
import { ConfigService } from '@leaa/api/src/modules/v1/config/config.service';
import { filenameAt1xToAt2x, isAt2x, logger, uuid, buildUrl, buildUrlAt2x } from '@leaa/api/src/utils';
import { filenameAt1xToAt2x, isAt2x, logger, uuid, genUrl, genUrlAt2x } from '@leaa/api/src/utils';

const CLS_NAME = 'SaveInOssAliyunService';

Expand Down Expand Up @@ -132,7 +132,7 @@ export class SaveInOssAliyunService {
}

async saveOssToLocal(attachment: Attachment): Promise<'ossIsSaveToLocal' | Error> {
await this.downloadFile(buildUrl(attachment) || '', (file) => {
await this.downloadFile(genUrl(attachment) || '', (file) => {
try {
mkdirp.sync(attachmentConfig.SAVE_DIR_BY_DISK);

Expand All @@ -144,7 +144,7 @@ export class SaveInOssAliyunService {
});

if (attachment.at2x) {
await this.downloadFile(buildUrlAt2x(attachment) || '', (file) => {
await this.downloadFile(genUrlAt2x(attachment) || '', (file) => {
try {
fs.writeFileSync(`${attachmentConfig.SAVE_DIR_BY_DISK}/${filenameAt1xToAt2x(attachment.filename)}`, file);
} catch (e) {
Expand Down Expand Up @@ -243,8 +243,8 @@ export class SaveInOssAliyunService {
// eslint-disable-next-line consistent-return
return this.attachmentRepo.save({
...attachmentData,
url: buildUrl(attachmentData as Attachment),
urlAt2x: buildUrlAt2x(attachmentData as Attachment),
url: genUrl(attachmentData as Attachment),
urlAt2x: genUrlAt2x(attachmentData as Attachment),
});
}

Expand Down
4 changes: 3 additions & 1 deletion packages/leaa-api/src/modules/v1/seed/seed.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export const rolesSeed = [

// prettier-ignore
export const usersSeed: UserCreateOneReq[] = [
{ email: 'superuser@local.com', name: 'Super User', password: 'RwurDVXqHvmUZZWzmrwLwKcrzyyDMKVsRJawqQUzZygEeqCorbfBf7fmxRZecNUx', status: 1, is_admin: 1, is_superuser: 1},
{ email: 'admin@local.com', name: 'Admin', password: 'h8Hx9qvPKoHMLQgj', status: 1, is_admin: 1, avatar_url: '' },
{ email: 'staff@local.com', name: 'Staff', password: '7PkQGjvHMMkoo4RZ', status: 1, is_admin: 1 },
{ email: 'disabled@local.com', name: 'Disabled', password: 'uUB3YGrdL3gJZYij', status: 1, is_admin: 1 },
Expand Down Expand Up @@ -253,8 +254,9 @@ export const permissionsToRoleSeed = [

// prettier-ignore
export const rolesToUserSeed = [
{ userEmail: 'admin@local.com', roleSlugs: ['admin', 'staff', 'attachment-manager'] },
{ userEmail: 'admin@local.com', roleSlugs: ['admin'] },
{ userEmail: 'staff@local.com', roleSlugs: ['staff', 'attachment-manager'] },
{ userEmail: 'superuser@local.com', roleSlugs: ['admin'] },
];

interface ICreateCategoryInput extends CategoryCreateOneReq {
Expand Down
3 changes: 1 addition & 2 deletions packages/leaa-api/src/modules/v1/typeorm/typeorm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
Action,
} from '@leaa/common/src/entrys';

import { UserSubscriber } from '@leaa/api/src/subscribers';
import { ConfigService } from '@leaa/api/src/modules/v1/config/config.service';

const CLS_NAME = 'TypeormService';
Expand Down Expand Up @@ -68,7 +67,7 @@ export class TypeormService implements TypeOrmOptionsFactory {
Verification,
Action,
],
subscribers: [UserSubscriber],
// subscribers: [],
};
}
}
18 changes: 13 additions & 5 deletions packages/leaa-api/src/modules/v1/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, UseGuards } from '@nestjs/common';
import { Controller, UseGuards, Headers } from '@nestjs/common';
import { Crud, CrudController, Override, ParsedRequest, CrudRequest, ParsedBody } from '@nestjsx/crud';

import { Permissions } from '@leaa/api/src/decorators';
import { Permissions, JwtUser } from '@leaa/api/src/decorators';
import { UserCreateOneReq, UserUpdateOneReq } from '@leaa/common/src/dtos/user';
import { JwtGuard, PermissionsGuard } from '@leaa/api/src/guards';
import { User } from '@leaa/common/src/entrys';
Expand All @@ -23,7 +23,10 @@ import { UserService } from './user.service';
query: {
maxLimit: 1000,
alwaysPaginate: true,
sort: [{ field: 'created_at', order: 'DESC' }],
sort: [
{ field: 'created_at', order: 'DESC' },
{ field: 'email', order: 'DESC' },
],
join: {
roles: { eager: true },
},
Expand All @@ -46,6 +49,7 @@ import { UserService } from './user.service';
},
})
@Controller('/v1/users')
// @ts-ignore
export class UserController implements CrudController<User> {
constructor(public service: UserService) {}

Expand All @@ -59,8 +63,12 @@ export class UserController implements CrudController<User> {
@Override('updateOneBase')
@UseGuards(JwtGuard, PermissionsGuard)
@Permissions('user.item-update')
updateOne(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: UserUpdateOneReq): Promise<User> {
return this.service.updateOne(req, dto);
updateOne(
@ParsedRequest() req: CrudRequest,
@ParsedBody() dto: UserUpdateOneReq,
@JwtUser() jwtUser: any,
): Promise<User> {
return this.service.updateOne(req, dto, jwtUser);
}

@Override('deleteOneBase')
Expand Down

0 comments on commit c317528

Please sign in to comment.