username support

This commit is contained in:
hay-kot 2021-05-25 19:43:58 -07:00
commit 7500e5f887
7 changed files with 64 additions and 14 deletions

View file

@ -377,6 +377,7 @@
"untagged-count": "Untagged {count}" "untagged-count": "Untagged {count}"
}, },
"user": { "user": {
"username": "Username",
"admin": "Admin", "admin": "Admin",
"are-you-sure-you-want-to-delete-the-link": "Are you sure you want to delete the link <b>{link}<b/>?", "are-you-sure-you-want-to-delete-the-link": "Are you sure you want to delete the link <b>{link}<b/>?",
"are-you-sure-you-want-to-delete-the-user": "Are you sure you want to delete the user <b>{activeName} ID: {activeId}<b/>?", "are-you-sure-you-want-to-delete-the-user": "Are you sure you want to delete the user <b>{activeName} ID: {activeId}<b/>?",

View file

@ -1,5 +1,33 @@
<template> <template>
<div> <div>
<v-app-bar color="primary">
<v-spacer></v-spacer>
<v-btn href="https://github.com/sponsors/hay-kot" target="_blank" class="mx-1" color="secondary">
<v-icon left>
mdi-heart
</v-icon>
Support
</v-btn>
<v-btn href="https://github.com/hay-kot" target="_blank" class="mx-1" color="secondary">
<v-icon left>
mdi-github
</v-icon>
Github
</v-btn>
<v-btn href="https://hay-kot.dev" target="_blank" class="mx-1" color="secondary">
<v-icon left>
mdi-account
</v-icon>
Portfolio
</v-btn>
<v-btn href="https://hay-kot.github.io/mealie/" target="_blank" class="mx-1" color="secondary">
<v-icon left>
mdi-folder-outline
</v-icon>
Docs
</v-btn>
<v-spacer></v-spacer>
</v-app-bar>
<v-card class="mt-3"> <v-card class="mt-3">
<v-card-title class="headline"> <v-card-title class="headline">
{{ $t("about.about-mealie") }} {{ $t("about.about-mealie") }}

View file

@ -67,7 +67,15 @@
</template> </template>
<template v-slot:bottom> <template v-slot:bottom>
<v-card-text> <v-card-text>
<v-form> <v-form ref="userUpdate">
<v-text-field
:label="$t('user.username')"
required
v-model="user.username"
:rules="[existsRule]"
validate-on-blur
>
</v-text-field>
<v-text-field <v-text-field
:label="$t('user.full-name')" :label="$t('user.full-name')"
required required
@ -151,6 +159,9 @@ export default {
this.user.avatar = avatar; this.user.avatar = avatar;
}, },
async updateUser() { async updateUser() {
if (!this.$refs.userUpdate.validate()) {
return;
}
this.loading = true; this.loading = true;
const response = await api.users.update(this.user); const response = await api.users.update(this.user);

View file

@ -28,8 +28,13 @@ def create_file_token(file_path: Path) -> bool:
def authenticate_user(session, email: str, password: str) -> UserInDB: def authenticate_user(session, email: str, password: str) -> UserInDB:
user: UserInDB = db.users.get(session, email, "email", any_case=True) user: UserInDB = db.users.get(session, email, "email", any_case=True)
if not user:
user = db.users.get(session, email, "username", any_case=True)
if not user: if not user:
return False return False
print(user)
if not verify_password(password, user.password): if not verify_password(password, user.password):
return False return False
return user return user

View file

@ -22,6 +22,11 @@ class User(SqlAlchemyBase, BaseMixins):
__tablename__ = "users" __tablename__ = "users"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
full_name = Column(String, index=True) full_name = Column(String, index=True)
username = Column(
String,
index=True,
unique=True,
)
email = Column(String, unique=True, index=True) email = Column(String, unique=True, index=True)
password = Column(String) password = Column(String)
group_id = Column(Integer, ForeignKey("groups.id")) group_id = Column(Integer, ForeignKey("groups.id"))
@ -32,16 +37,7 @@ class User(SqlAlchemyBase, BaseMixins):
) )
def __init__( def __init__(
self, self, session, full_name, email, password, group: str = settings.DEFAULT_GROUP, admin=False, **_
session,
full_name,
email,
password,
group: str = settings.DEFAULT_GROUP,
admin=False,
id=None,
*args,
**kwargs
) -> None: ) -> None:
group = group or settings.DEFAULT_GROUP group = group or settings.DEFAULT_GROUP
@ -51,12 +47,19 @@ class User(SqlAlchemyBase, BaseMixins):
self.admin = admin self.admin = admin
self.password = password self.password = password
def update(self, full_name, email, group, admin, session=None, id=None, password=None, *args, **kwargs): if self.username is None:
self.username = full_name
def update(self, full_name, email, group, admin, username, session=None, id=None, password=None, *args, **kwargs):
self.username = username
self.full_name = full_name self.full_name = full_name
self.email = email self.email = email
self.group = Group.get_ref(session, group) self.group = Group.get_ref(session, group)
self.admin = admin self.admin = admin
if self.username is None:
self.username = full_name
if password: if password:
self.password = password self.password = password

View file

@ -23,7 +23,7 @@ def get_token(
email = data.username email = data.username
password = data.password password = data.password
user = authenticate_user(session, email, password) user: UserInDB = authenticate_user(session, email, password)
if not user: if not user:
background_tasks.add_task( background_tasks.add_task(
@ -34,7 +34,7 @@ def get_token(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
access_token = security.create_access_token(dict(sub=email)) access_token = security.create_access_token(dict(sub=user.email))
return {"access_token": access_token, "token_type": "bearer"} return {"access_token": access_token, "token_type": "bearer"}

View file

@ -43,6 +43,7 @@ class GroupBase(CamelModel):
class UserBase(CamelModel): class UserBase(CamelModel):
username: Optional[str]
full_name: Optional[str] = None full_name: Optional[str] = None
email: constr(to_lower=True, strip_whitespace=True) email: constr(to_lower=True, strip_whitespace=True)
admin: bool admin: bool
@ -59,6 +60,7 @@ class UserBase(CamelModel):
} }
schema_extra = { schema_extra = {
"username": "ChangeMe",
"fullName": "Change Me", "fullName": "Change Me",
"email": "changeme@email.com", "email": "changeme@email.com",
"group": settings.DEFAULT_GROUP, "group": settings.DEFAULT_GROUP,