|
3 | 3 | from django.contrib.auth.models import Permission
|
4 | 4 |
|
5 | 5 | from django.conf import settings
|
| 6 | +from graphql import GraphQLError |
6 | 7 |
|
7 | 8 | from nxtbn.users.admin_types import PermissionType
|
8 | 9 | from nxtbn.users.api.storefront.serializers import JwtBasicUserSerializer
|
@@ -141,9 +142,35 @@ def mutate(self, info, user_id, permission_codename):
|
141 | 142 | return TogglePermissionMutation(success=True, message="Permission assigned successfully")
|
142 | 143 |
|
143 | 144 |
|
| 145 | + |
| 146 | +class ChangeUserPasswordMutation(graphene.Mutation): |
| 147 | + class Arguments: |
| 148 | + user_id = graphene.ID(required=True) |
| 149 | + password = graphene.String(required=True) |
| 150 | + confirm_password = graphene.String(required=True) |
| 151 | + |
| 152 | + success = graphene.Boolean() |
| 153 | + message = graphene.String() |
| 154 | + |
| 155 | + def mutate(self, info, user_id, password, confirm_password): |
| 156 | + |
| 157 | + try: |
| 158 | + user = User.objects.get(id=user_id) |
| 159 | + except User.DoesNotExist: |
| 160 | + raise GraphQLError("User not found") |
| 161 | + |
| 162 | + if password != confirm_password: |
| 163 | + raise GraphQLError("Passwords do not match") |
| 164 | + |
| 165 | + user.set_password(password) |
| 166 | + user.save() |
| 167 | + |
| 168 | + return ChangeUserPasswordMutation(success=True, message="Password changed successfully") |
| 169 | + |
144 | 170 | class AdminUserMutation(graphene.ObjectType):
|
145 | 171 | login = AdminLoginMutation.Field()
|
146 | 172 | refresh_token = AdminTokenRefreshMutation.Field()
|
147 | 173 | toggle_permission = TogglePermissionMutation.Field()
|
| 174 | + change_user_password = ChangeUserPasswordMutation.Field() |
148 | 175 |
|
149 | 176 |
|
0 commit comments