Skip to content

Commit

Permalink
Return error when failed to get user from database (#700)
Browse files Browse the repository at this point in the history
* Return error when failed to get user from database

* Use CCNET_ERR_INTERNAL

---------

Co-authored-by: 杨赫然 <[email protected]>
  • Loading branch information
feiniks and 杨赫然 authored Sep 18, 2024
1 parent f27ab08 commit 72753e1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
8 changes: 3 additions & 5 deletions common/rpc-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#define DEBUG_FLAG SEAFILE_DEBUG_OTHER
#include "log.h"

#define CCNET_ERR_INTERNAL 500

#ifndef SEAFILE_SERVER
#include "../daemon/vc-utils.h"

Expand Down Expand Up @@ -4726,7 +4724,7 @@ ccnet_rpc_get_emailuser (const char *email, GError **error)
CcnetUserManager *user_mgr = seaf->user_mgr;
CcnetEmailUser *emailuser = NULL;

emailuser = ccnet_user_manager_get_emailuser (user_mgr, email);
emailuser = ccnet_user_manager_get_emailuser (user_mgr, email, error);

return (GObject *)emailuser;
}
Expand All @@ -4742,7 +4740,7 @@ ccnet_rpc_get_emailuser_with_import (const char *email, GError **error)
CcnetUserManager *user_mgr = seaf->user_mgr;
CcnetEmailUser *emailuser = NULL;

emailuser = ccnet_user_manager_get_emailuser_with_import (user_mgr, email);
emailuser = ccnet_user_manager_get_emailuser_with_import (user_mgr, email, error);

return (GObject *)emailuser;
}
Expand Down Expand Up @@ -5448,7 +5446,7 @@ ccnet_rpc_get_org_emailusers (const char *url_prefix, int start , int limit,
while (ptr) {
char *email = ptr->data;
CcnetEmailUser *emailuser = ccnet_user_manager_get_emailuser (user_mgr,
email);
email, NULL);
if (emailuser != NULL) {
ret = g_list_prepend (ret, emailuser);
}
Expand Down
38 changes: 29 additions & 9 deletions common/user-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1147,26 +1147,41 @@ get_ldap_emailuser_cb (CcnetDBRow *row, void *data)
static CcnetEmailUser*
get_emailuser (CcnetUserManager *manager,
const char *email,
gboolean import)
gboolean import,
GError **error)
{
CcnetDB *db = manager->priv->db;
char *sql;
CcnetEmailUser *emailuser = NULL;
char *email_down;
int rc;

sql = "SELECT e.id, e.email, is_staff, is_active, ctime, passwd, reference_id, role "
" FROM EmailUser e LEFT JOIN UserRole ON e.email = UserRole.email "
" WHERE e.email=?";
if (seaf_db_statement_foreach_row (db, sql, get_emailuser_cb, &emailuser,
1, "string", email) > 0) {
rc = seaf_db_statement_foreach_row (db, sql, get_emailuser_cb, &emailuser,
1, "string", email);
if (rc > 0) {
return emailuser;
} else if (rc < 0) {
if (error) {
g_set_error (error, CCNET_DOMAIN, CCNET_ERR_INTERNAL, "Database error");
}
return NULL;
}

email_down = g_ascii_strdown (email, strlen(email));
if (seaf_db_statement_foreach_row (db, sql, get_emailuser_cb, &emailuser,
1, "string", email_down) > 0) {
rc = seaf_db_statement_foreach_row (db, sql, get_emailuser_cb, &emailuser,
1, "string", email_down);
if (rc > 0) {
g_free (email_down);
return emailuser;
} else if (rc < 0) {
if (error) {
g_set_error (error, CCNET_DOMAIN, CCNET_ERR_INTERNAL, "Database error");
}
g_free (email_down);
return NULL;
}

#ifdef HAVE_LDAP
Expand All @@ -1179,6 +1194,9 @@ get_emailuser (CcnetUserManager *manager,
get_ldap_emailuser_cb,
&emailuser, 1, "string", email_down);
if (ret < 0) {
if (error) {
g_set_error (error, CCNET_DOMAIN, CCNET_ERR_INTERNAL, "Database error");
}
ccnet_warning ("get ldapuser from db failed.\n");
g_free (email_down);
return NULL;
Expand Down Expand Up @@ -1236,16 +1254,18 @@ get_emailuser (CcnetUserManager *manager,

CcnetEmailUser*
ccnet_user_manager_get_emailuser (CcnetUserManager *manager,
const char *email)
const char *email,
GError **error)
{
return get_emailuser (manager, email, FALSE);
return get_emailuser (manager, email, FALSE, error);
}

CcnetEmailUser*
ccnet_user_manager_get_emailuser_with_import (CcnetUserManager *manager,
const char *email)
const char *email,
GError **error)
{
return get_emailuser (manager, email, TRUE);
return get_emailuser (manager, email, TRUE, error);
}

CcnetEmailUser*
Expand Down
5 changes: 3 additions & 2 deletions common/user-mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ ccnet_user_manager_validate_emailuser (CcnetUserManager *manager,
const char *passwd);

CcnetEmailUser*
ccnet_user_manager_get_emailuser (CcnetUserManager *manager, const char *email);
ccnet_user_manager_get_emailuser (CcnetUserManager *manager, const char *email, GError **error);

CcnetEmailUser*
ccnet_user_manager_get_emailuser_with_import (CcnetUserManager *manager,
const char *email);
const char *email,
GError **error);
CcnetEmailUser*
ccnet_user_manager_get_emailuser_by_id (CcnetUserManager *manager, int id);

Expand Down
2 changes: 1 addition & 1 deletion fuse/getattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static int getattr_user(SeafileSession *seaf, const char *user, struct stat *stb
{
CcnetEmailUser *emailuser;

emailuser = ccnet_user_manager_get_emailuser (seaf->user_mgr, user);
emailuser = ccnet_user_manager_get_emailuser (seaf->user_mgr, user, NULL);
if (!emailuser) {
return -ENOENT;
}
Expand Down
2 changes: 1 addition & 1 deletion fuse/readdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static int readdir_user(SeafileSession *seaf, const char *user,
GList *list = NULL, *p;
GString *name;

emailuser = ccnet_user_manager_get_emailuser (seaf->user_mgr, user);
emailuser = ccnet_user_manager_get_emailuser (seaf->user_mgr, user, NULL);
if (!emailuser) {
return -ENOENT;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ traverse_directory_win32 (wchar_t *path_w,

#define CCNET_DOMAIN g_quark_from_string("ccnet")

#define CCNET_ERR_INTERNAL 500


struct timeval timeval_from_msec (uint64_t milliseconds);

Expand Down

0 comments on commit 72753e1

Please sign in to comment.