2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <cynara-client.h>
20 #include "PeerCreds.h"
21 #include "Privilege.h"
23 #define PRIV_PREFIX "http://tizen.org/privilege/"
24 #define CACHE_SIZE 100
26 class PermissionChecker {
32 cynara_configuration *conf;
34 int err = cynara_configuration_create(&conf);
35 IF_FAIL_VOID_TAG(err == CYNARA_API_SUCCESS, _E, "Cynara configuration creation failed");
37 err = cynara_configuration_set_cache_size(conf, CACHE_SIZE);
38 if (err != CYNARA_API_SUCCESS) {
39 _E("Cynara cache size set failed");
40 cynara_configuration_destroy(conf);
44 err = cynara_initialize(&__cynara, conf);
45 cynara_configuration_destroy(conf);
46 if (err != CYNARA_API_SUCCESS) {
47 _E("Cynara initialization failed");
52 _I("Cynara initialized");
58 cynara_finish(__cynara);
60 _I("Cynara deinitialized");
64 static PermissionChecker& getInstance()
66 static PermissionChecker instance;
70 bool hasPermission(const ctx::Credentials *creds, const char *privilege)
72 IF_FAIL_RETURN_TAG(__cynara, false, _E, "Cynara not initialized");
73 int ret = cynara_check(__cynara, creds->client, creds->session, creds->user, privilege);
74 return (ret == CYNARA_API_ACCESS_ALLOWED);
78 bool ctx::privilege_manager::isAllowed(const ctx::Credentials *creds, const char *privilege)
80 IF_FAIL_RETURN(creds && privilege, true);
82 std::string priv = PRIV_PREFIX;
85 return PermissionChecker::getInstance().hasPermission(creds, priv.c_str());