Feature: Allow password change
This commit is contained in:
parent
72b0e0e97d
commit
27205f7790
@ -42,31 +42,27 @@ def save(
|
||||
)
|
||||
|
||||
|
||||
@router.get("/me", response_model=schemas.User)
|
||||
@router.get("/me", response_model=schemas.UserMe)
|
||||
def show_me(
|
||||
user: UserToken = Depends(get_user),
|
||||
) -> schemas.User:
|
||||
) -> schemas.UserMe:
|
||||
with SessionFuture() as db:
|
||||
item = db.execute(select(User).where(User.id == user.id_)).scalar_one()
|
||||
return user_info(item, db, user)
|
||||
return user_me(item)
|
||||
|
||||
|
||||
@router.put("/me", response_model=schemas.User)
|
||||
@router.put("/me", response_model=schemas.UserMe)
|
||||
def update_me(
|
||||
data: schemas.UserIn,
|
||||
user: UserToken = Depends(get_user),
|
||||
) -> schemas.User:
|
||||
) -> schemas.UserMe:
|
||||
try:
|
||||
with SessionFuture() as db:
|
||||
item: User = db.execute(select(User).where(User.id == user.id_)).scalar_one()
|
||||
if "users" in user.permissions:
|
||||
item.name = data.name
|
||||
item.locked_out = data.locked_out
|
||||
add_roles(item, data.roles, db)
|
||||
if data.password and item.password != data.password:
|
||||
item.password = data.password
|
||||
db.commit()
|
||||
return user_info(item, db, user)
|
||||
return user_me(item)
|
||||
except SQLAlchemyError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
@ -184,6 +180,16 @@ def user_info(item: User, db: Session, user: UserToken) -> schemas.User:
|
||||
)
|
||||
|
||||
|
||||
def user_me(item: User) -> schemas.UserMe:
|
||||
return schemas.UserMe(
|
||||
id="me",
|
||||
name=item.name,
|
||||
password="",
|
||||
lockedOut=item.locked_out,
|
||||
roles=[],
|
||||
)
|
||||
|
||||
|
||||
def blank_user_info(db: Session) -> schemas.UserIn:
|
||||
return schemas.UserIn(
|
||||
name="",
|
||||
|
@ -27,6 +27,14 @@ class User(UserIn):
|
||||
alias_generator = to_camel
|
||||
|
||||
|
||||
class UserMe(UserIn):
|
||||
id_: str
|
||||
|
||||
class Config:
|
||||
anystr_strip_whitespace = True
|
||||
alias_generator = to_camel
|
||||
|
||||
|
||||
class UserLink(BaseModel):
|
||||
id_: Optional[uuid.UUID]
|
||||
name: Optional[str]
|
||||
|
@ -232,6 +232,15 @@
|
||||
>
|
||||
<h3 class="item-name">Users</h3>
|
||||
</mat-card>
|
||||
<mat-card
|
||||
fxLayout="column"
|
||||
class="square-button"
|
||||
matRipple
|
||||
*ngIf="auth.allowed('authenticated')"
|
||||
[routerLink]="['/', 'users', 'me']"
|
||||
>
|
||||
<h3 class="item-name">Change Password</h3>
|
||||
</mat-card>
|
||||
<mat-card
|
||||
fxLayout="column"
|
||||
class="square-button"
|
||||
|
@ -73,7 +73,12 @@ export class UserDetailComponent implements OnInit, AfterViewInit {
|
||||
this.ser.saveOrUpdate(this.getItem()).subscribe(
|
||||
() => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigateByUrl('/users');
|
||||
console.log(this.item.id);
|
||||
if ((this.item.id as string) === 'me') {
|
||||
this.router.navigateByUrl('/');
|
||||
} else {
|
||||
this.router.navigateByUrl('/users');
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Error', error);
|
||||
|
Loading…
Reference in New Issue
Block a user