Add getting privileges in the same privacy group 07/175807/3 accepted/tizen/unified/20180420.081831 submit/tizen/20180419.074918
authorYunjin Lee <yunjin-.lee@samsung.com>
Thu, 12 Apr 2018 10:10:10 +0000 (19:10 +0900)
committerYunjin Lee <yunjin-.lee@samsung.com>
Mon, 16 Apr 2018 07:51:09 +0000 (16:51 +0900)
- For privacy status checking, Settings or askuser requires
all privileges of the same privacy group when a privilege is given so
added API to get them at once.

Change-Id: I0b866c889b3eb6dfaa1db6246936446aadd2c1d5
Signed-off-by: Yunjin Lee <yunjin-.lee@samsung.com>
capi/include/privilege_db_manager.h
capi/include/privilege_info.h
capi/src/privilege_db_manager.c
capi/src/privilege_info.c
test/tc-privilege-db-manager.c
test/tc-privilege-info.c

index 645c228..c35ce3f 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright(c) 2013-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright(c) 2013-2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0(the License);
  * you may not use this file except in compliance with the License.
@@ -190,6 +190,20 @@ int privilege_db_manager_get_privilege_list_by_privacy(const char* privacy_name,
 int privilege_db_manager_get_privacy_by_privilege(const char* privilege, char** privacy_name);
 
 /**
+ * @brief Get privilege list of the same privacy group that the given privilege is included in.
+ * @param [in]  privilege The privilege
+ * @param [out] privilege_list The privilege list of the same privacy group.
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #PRIVILEGE_DB_MANAGER_ERR_NONE  Successful.
+ * @retval #PRIVILEGE_DB_MANAGER_ERR_INVALID_QUERY Failed to prepare sql query.
+ * @retval #PRIVILEGE_DB_MANAGER_ERR_DB_NOENTRY DB not exist.
+ * @retval #PRIVILEGE_DB_MANAGER_ERR_CONNECTION_FAIL DB exist but failed to open DB.
+ * @retval #PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT No data selected by given qeury.
+ */
+int privilege_db_manager_get_same_privacy_grouped_privileges(const char* privilege, GList **privilege_list);
+
+
+/**
  * @brief Get privacy ID that the given privilege is included in.
  * @param [in]  privilege The privilege
  * @param [out] privacy_id The privacy ID
index 908af1f..5a9dd14 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2013-2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
@@ -221,6 +221,19 @@ EXPORT_API int privilege_info_get_black_list(int uid, privilege_manager_package_
  */
 EXPORT_API int privilege_info_get_mdm_black_list(uid_t uid, GList **privilege_list);
 
+/**
+ * @brief Get privilege list of the same privacy group that the given privilege is included in.
+ * @remarks @a privilege_list must be released with g_list_free() by you.
+ * @remarks @a privilege_list is NULL when the given privilege is not a privacy privilege.
+ * @param [in] privilege privilege
+ * @param [out] privilege_list The privilege list of the same privacy group
+ * @return 0 on success, otherwise a nonzero error value.
+ * @retval #PRVMGR_ERR_NONE Successful
+ * @retval #PRVMGR_ERR_INTERNAL_ERROR Internal error
+ * @retval #PRVMGR_ERR_INVALID_PARAMETER Invalid parameter
+ */
+EXPORT_API int privilege_info_get_same_privacy_grouped_privileges(const char* privilege, GList **privilege_list);
+
 #ifdef __cplusplus
 }
 #endif
index 90b5ee7..0ae9c8b 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright(c) 2013-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright(c) 2013-2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0(the License);
  * you may not use this file except in compliance with the License.
@@ -663,6 +663,40 @@ int privilege_db_manager_get_privacy_by_privilege(const char* privilege, char**
        return ret;
 }
 
+int privilege_db_manager_get_same_privacy_grouped_privileges(const char* privilege, GList** privilege_list)
+{
+       sqlite3 *db = NULL;
+       sqlite3_stmt *stmt = NULL;
+       int ret = __initialize_db(PRIVILEGE_DB_TYPE_INFO, &db);
+       if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE)
+               return ret;
+
+       char *sql = sqlite3_mprintf("select distinct privilege_name from privilege_info where privacy_name=(select distinct privacy_name from privilege_info where privilege_name=%Q and is_privacy=1)", privilege);
+       TryReturn(sql != NULL, __finalize_db(db, stmt, sql), PRIVILEGE_DB_MANAGER_ERR_INVALID_QUERY, "[DB_FAIL] sqlite3_mprintf failed");
+       ret = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
+       TryReturn(ret == SQLITE_OK, __finalize_db(db, stmt, sql), PRIVILEGE_DB_MANAGER_ERR_INVALID_QUERY, "[DB_FAIL] fail to prepare database: %s", sqlite3_errmsg(db));
+
+       GList* temp_privilege_list = NULL;
+       do {
+               ret = sqlite3_step(stmt);
+               if (ret == SQLITE_ROW) {
+                       char *privilege_name = strdup((char *)sqlite3_column_text(stmt, 0));
+                       TryReturn(privilege_name != NULL, __finalize_db(db, stmt, sql); g_list_free_full(temp_privilege_list, free), PRIVILEGE_DB_MANAGER_ERR_OUT_OF_MEMORY, "strdup failed");
+                       temp_privilege_list = g_list_append(temp_privilege_list, privilege_name);
+               }
+       } while (ret == SQLITE_ROW);
+
+       __finalize_db(db, stmt, sql);
+
+       if (temp_privilege_list == NULL)
+               return PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT;
+
+       *privilege_list = temp_privilege_list;
+
+       return PRIVILEGE_DB_MANAGER_ERR_NONE;
+
+}
+
 int privilege_db_manager_get_privacy_id_by_privilege(const char* privilege, int *privacy_id)
 {
        sqlite3 *db = NULL;
index 24089a5..262f123 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright(c) 2013-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright(c) 2013-2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0(the License);
  * you may not use this file except in compliance with the License.
@@ -654,3 +654,13 @@ int privilege_info_get_mdm_black_list(uid_t uid, GList **privilege_list)
                ret = PRVMGR_ERR_INTERNAL_ERROR;
        return ret;
 }
+
+int privilege_info_get_same_privacy_grouped_privileges(const char* privilege, GList **privilege_list)
+{
+       TryReturn(privilege != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] privilege is null");
+       int ret = privilege_db_manager_get_same_privacy_grouped_privileges(privilege, privilege_list);
+       if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE || ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT)
+               return PRVMGR_ERR_NONE;
+       else
+               return PRVMGR_ERR_INTERNAL_ERROR;
+}
index f6e1dad..44fa896 100755 (executable)
@@ -596,6 +596,94 @@ void __test_privilege_db_manager_is_user_settable()
        __print_line();
 }
 
+void __test_privilege_db_manager_get_same_privacy_grouped_privileges()
+{
+       int ret = 0;
+       GList *privilege_list = NULL;
+
+       __print_line();
+       printf("privilege = http://tizen.org/privilege/account.read\n");
+       ret = privilege_db_manager_get_same_privacy_grouped_privileges("http://tizen.org/privilege/account.read", &privilege_list);
+
+       if (ENABLE_ASKUSER) {
+               printf("privacy = account\n");
+               if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE && privilege_list != NULL) {
+                       success_cnt++;
+                       __color_to_green();
+                       printf("success\n");
+                       __color_to_origin();
+               } else {
+                       fail_cnt++;
+                       __color_to_red();
+                       printf("failed. ret = %s\n", __get_result_string('m', ret));
+                       __color_to_origin();
+               }
+       } else {
+               printf("not privacy\n");
+               if (ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT && privilege_list == NULL) {
+                       success_cnt++;
+                       __color_to_green();
+                       printf("success\n");
+                       __color_to_origin();
+               } else {
+                       fail_cnt++;
+                       __color_to_red();
+                       printf("failed. ret = %s\n", __get_result_string('d', ret));
+                       __color_to_origin();
+               }
+       }
+
+       if (privilege_list != NULL) {
+               __print_glist(privilege_list);
+               g_list_free_full(privilege_list, free);
+               privilege_list = NULL;
+       }
+
+       __print_line();
+       printf("privilege = http://tizen.org/privilege/internet\n");
+       printf("not privacy\n");
+
+       ret = privilege_db_manager_get_same_privacy_grouped_privileges("http://tizen.org/privilege/internet", &privilege_list);
+       if (ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT && privilege_list == NULL) {
+               success_cnt++;
+               __color_to_green();
+               printf("success\n");
+               __color_to_origin();
+       } else {
+               fail_cnt++;
+               __color_to_red();
+               printf("failed. internet privilege is not privacy. ret = %s\n", __get_result_string('d', ret));
+               __color_to_origin();
+       }
+
+       if (privilege_list != NULL) {
+               __print_glist(privilege_list);
+               g_list_free_full(privilege_list, free);
+               privilege_list = NULL;
+       }
+       __print_line();
+       printf("privilege = http://tizen.org/privilege/internal/buxton/message.read\n");
+       printf("not privacy\n");
+
+       ret = privilege_db_manager_get_same_privacy_grouped_privileges("http://tizen.org/privilege/internal/buxton/message.read", &privilege_list);
+       if (ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT && privilege_list == NULL) {
+               success_cnt++;
+               __color_to_green();
+               printf("success\n");
+               __color_to_origin();
+       } else {
+               fail_cnt++;
+               __color_to_red();
+               printf("failed. internal/buxton/message.read privilege is not privacy. ret = %s\n", __get_result_string('d', ret));
+               __color_to_origin();
+       }
+
+       if (privilege_list != NULL) {
+               __print_glist(privilege_list);
+               g_list_free_full(privilege_list, free);
+       }
+}
+
 int main()
 {
        __tcinfo(function, "privilege_db_manager_get_privilege_list");
@@ -622,6 +710,9 @@ int main()
        __tcinfo(function, "privilege_db_manager_is_user_settable");
        __test_privilege_db_manager_is_user_settable();
 
+       __tcinfo(function, "privilege_db_manager_get_same_privacy_grouped_privileges");
+       __test_privilege_db_manager_get_same_privacy_grouped_privileges();
+
        __color_to_green();
        printf("Test Complete\n");
        printf("success : %d, ", success_cnt);
index 621c61b..840f93c 100755 (executable)
@@ -688,6 +688,95 @@ void __test_privilege_info_is_user_settable()
        __print_line();
 }
 
+void __test_privilege_info_get_same_privacy_grouped_privileges()
+{
+       int ret = 0;
+       GList *privilege_list = NULL;
+
+       __print_line();
+       printf("privilege = http://tizen.org/privilege/account.read\n");
+       ret = privilege_info_get_same_privacy_grouped_privileges("http://tizen.org/privilege/account.read", &privilege_list);
+
+       if (ENABLE_ASKUSER) {
+               printf("privacy = account\n");
+               if (ret == PRVMGR_ERR_NONE && privilege_list != NULL) {
+                       success_cnt++;
+                       __color_to_green();
+                       printf("success\n");
+                       __color_to_origin();
+               } else {
+                       fail_cnt++;
+                       __color_to_red();
+                       printf("failed. ret = %s\n", __get_result_string('m', ret));
+                       __color_to_origin();
+               }
+       } else {
+               printf("not privacy\n");
+               if (ret == PRVMGR_ERR_NONE && privilege_list == NULL) {
+                       success_cnt++;
+                       __color_to_green();
+                       printf("success\n");
+                       __color_to_origin();
+               } else {
+                       fail_cnt++;
+                       __color_to_red();
+                       printf("failed. ret = %s\n", __get_result_string('m', ret));
+                       __color_to_origin();
+               }
+       }
+
+       if (privilege_list != NULL) {
+               __print_glist(privilege_list);
+               g_list_free_full(privilege_list, free);
+               privilege_list = NULL;
+       }
+
+       __print_line();
+       printf("privilege = http://tizen.org/privilege/internet\n");
+       printf("not privacy\n");
+
+       ret = privilege_info_get_same_privacy_grouped_privileges("http://tizen.org/privilege/internet", &privilege_list);
+       if (ret == PRVMGR_ERR_NONE && privilege_list == NULL) {
+               success_cnt++;
+               __color_to_green();
+               printf("success\n");
+               __color_to_origin();
+       } else {
+               fail_cnt++;
+               __color_to_red();
+               printf("failed. internet privilege is not privacy. ret = %s\n", __get_result_string('m', ret));
+               __color_to_origin();
+       }
+
+       if (privilege_list != NULL) {
+               __print_glist(privilege_list);
+               g_list_free_full(privilege_list, free);
+               privilege_list = NULL;
+       }
+
+       __print_line();
+       printf("privilege = http://tizen.org/privilege/internal/buxton/message.read\n");
+       printf("not privacy\n");
+
+       ret = privilege_info_get_same_privacy_grouped_privileges("http://tizen.org/privilege/internal/buxton/message.read", &privilege_list);
+       if (ret == PRVMGR_ERR_NONE && privilege_list == NULL) {
+               success_cnt++;
+               __color_to_green();
+               printf("success\n");
+               __color_to_origin();
+       } else {
+               fail_cnt++;
+               __color_to_red();
+               printf("failed. internal/buxton/message.read privilege is not privacy. ret = %s\n", __get_result_string('m', ret));
+               __color_to_origin();
+       }
+
+       if (privilege_list != NULL) {
+               __print_glist(privilege_list);
+               g_list_free_full(privilege_list, free);
+       }
+}
+
 int main()
 {
        __tcinfo(function, "privilege_info_is_privacy");
@@ -726,6 +815,9 @@ int main()
        __tcinfo(function, "privilege_info_is_user_settable");
        __test_privilege_info_is_user_settable();
 
+       __tcinfo(function, "privilege_info_get_same_privacy_grouped_privileges");
+       __test_privilege_info_get_same_privacy_grouped_privileges();
+
        __color_to_green();
        printf("Test Complete\n");
        printf("success : %d, ", success_cnt);