Skip to content

Commit

Permalink
wip: admin verify user feature
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Sep 29, 2024
1 parent d54b2f2 commit 62da1c0
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 150 deletions.
2 changes: 2 additions & 0 deletions api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ type Mutation {
adminUpdateRole(input: AdminUpdateRoleInput!, roleId: String!): Role
adminUpdateUser(input: AdminUpdateUserInput!, userId: String!): User
adminVerifyNetworkAssets: Boolean
adminVerifyUser(userId: String!): Boolean
anonVerifyIdentityChallenge(input: VerifyIdentityChallengeInput!): IdentityChallenge
logout: Boolean
userAddCommunityMember(communityId: String!, input: UserAddCommunityMemberInput!): CommunityMember
Expand Down Expand Up @@ -783,6 +784,7 @@ type User {
name: String
private: Boolean
profileUrl: String!
pubkeyProfile: String
role: UserRole
status: UserStatus
updatedAt: DateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ export class ApiUserDataAdminService {
username: newUsername,
})
}

async verifyUser(userId: string) {
return this.data.verify(userId)
}
}
33 changes: 32 additions & 1 deletion libs/api/user/data-access/src/lib/api-user-data.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { ApiCoreService, PagingInputFields } from '@pubkey-link/api-core-data-access'
import { PubKeyIdentityProvider } from '@pubkey-program-library/anchor'
import { UserPaging } from './entity/user.entity'

@Injectable()
Expand All @@ -25,7 +26,7 @@ export class ApiUserDataService {
}

async findOne(userId: string) {
const found = await this.core.data.user.findUnique({ where: { id: userId } })
const found = await this.core.data.user.findUnique({ where: { id: userId }, include: { identities: true } })
if (!found) {
throw new Error(`User ${userId} not found`)
}
Expand All @@ -50,4 +51,34 @@ export class ApiUserDataService {
}
return true
}

async verify(userId: string) {
const found = await this.findOne(userId)
if (!found.identities.length) {
throw new Error(`Can't verify a user without identities`)
}
let foundPubkeyProfile = null
for (const identity of found.identities) {
console.log(`Checking ${identity.provider} ${identity.providerId}`)
foundPubkeyProfile = await this.core.protocol.getProfileByProvider({
provider: identity.provider as PubKeyIdentityProvider,
providerId: identity.providerId,
})
if (foundPubkeyProfile) {
console.log(`We found one, we break!`)
break
}
}
if (foundPubkeyProfile) {
if (found.pubkeyProfile !== foundPubkeyProfile.publicKey.toString()) {
const updated = await this.core.data.user.update({
where: { id: found.id },
data: { pubkeyProfile: foundPubkeyProfile.publicKey.toString() },
})

console.log(`Updated ${updated.username}, attached pubkey profile ${updated.pubkeyProfile}`)
}
}
return true
}
}
2 changes: 2 additions & 0 deletions libs/api/user/data-access/src/lib/entity/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class User {
@Field({ nullable: true })
name?: string | null
@Field({ nullable: true })
pubkeyProfile?: string | null
@Field({ nullable: true })
username!: string
@HideField()
identities?: unknown[] | null
Expand Down
5 changes: 5 additions & 0 deletions libs/api/user/feature/src/lib/api-admin-user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ export class ApiAdminUserResolver {
adminUpdateUser(@Args('userId') userId: string, @Args('input') input: AdminUpdateUserInput) {
return this.service.admin.updateUser(userId, input)
}

@Mutation(() => Boolean, { nullable: true })
adminVerifyUser(@Args('userId') userId: string) {
return this.service.admin.verifyUser(userId)
}
}
Loading

0 comments on commit 62da1c0

Please sign in to comment.