>From 76609774e899e60016635c0feafe6c3022dc9a31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzidek@redhat.com>
Date: Wed, 24 Jun 2015 18:03:49 +0200
Subject: [PATCH] views: Add is_default_view helper function

Ticket:
https://fedorahosted.org/sssd/ticket/2641
---
 src/db/sysdb.h                     | 11 +++++++++++
 src/db/sysdb_subdomains.c          |  3 +--
 src/providers/ipa/ipa_id.c         |  9 +++------
 src/providers/ipa/ipa_s2n_exop.c   | 14 ++++----------
 src/providers/ipa/ipa_subdomains.c |  5 ++---
 src/providers/ipa/ipa_views.c      |  2 +-
 6 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 4dc382f..fbc0185 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -461,6 +461,17 @@ errno_t sysdb_update_view_name(struct sysdb_ctx *sysdb, const char *view_name);
 errno_t sysdb_get_view_name(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
                             char **view_name);
 
+static inline bool is_default_view(const char *view_name)
+{
+    /* NULL is treated as default */
+    if (view_name == NULL
+            || strcmp(view_name, SYSDB_DEFAULT_VIEW_NAME) == 0) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
 errno_t sysdb_delete_view_tree(struct sysdb_ctx *sysdb, const char *view_name);
 
 errno_t sysdb_invalidate_overrides(struct sysdb_ctx *sysdb);
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
index 6d9e9e5..142520c 100644
--- a/src/db/sysdb_subdomains.c
+++ b/src/db/sysdb_subdomains.c
@@ -552,8 +552,7 @@ errno_t sysdb_master_domain_update(struct sss_domain_info *domain)
      * Currently changing the view is not supported hence we have to check for
      * changes and error out accordingly.
      */
-    if (ret == ENOENT || view_name == NULL
-            || strcmp(view_name, SYSDB_DEFAULT_VIEW_NAME) == 0) {
+    if (ret == ENOENT || is_default_view(view_name)) {
         /* handle default view */
         if (domain->has_views) {
             DEBUG(SSSDBG_CRIT_FAILURE,
diff --git a/src/providers/ipa/ipa_id.c b/src/providers/ipa/ipa_id.c
index 24dfe32..e81ccb3 100644
--- a/src/providers/ipa/ipa_id.c
+++ b/src/providers/ipa/ipa_id.c
@@ -445,7 +445,7 @@ static void ipa_initgr_get_overrides_override_done(struct tevent_req *subreq)
         return;
     }
 
-    if (strcmp(state->ipa_ctx->view_name, SYSDB_DEFAULT_VIEW_NAME) == 0) {
+    if (is_default_view(state->ipa_ctx->view_name)) {
         ret = sysdb_apply_default_override(state->user_dom, override_attrs,
                                        state->groups[state->group_idx]->dn);
     } else {
@@ -634,10 +634,8 @@ ipa_id_get_account_info_send(TALLOC_CTX *memctx, struct tevent_context *ev,
      * - there is no view set of it is the default view
      * - if the EXTRA_INPUT_MAYBE_WITH_VIEW flag is not set
      */
-    if (state->ipa_ctx->view_name == NULL
+    if (is_default_view(state->ipa_ctx->view_name)
             || state->ar->filter_type == BE_FILTER_SECID
-            || strcmp(state->ipa_ctx->view_name,
-                      SYSDB_DEFAULT_VIEW_NAME) == 0
             || state->ar->extra_value == NULL
             || strcmp(state->ar->extra_value,
                       EXTRA_INPUT_MAYBE_WITH_VIEW) != 0 ) {
@@ -842,8 +840,7 @@ static void ipa_id_get_account_info_orig_done(struct tevent_req *subreq)
     }
 
 
-    if (state->ipa_ctx->view_name != NULL &&
-            strcmp(state->ipa_ctx->view_name, SYSDB_DEFAULT_VIEW_NAME) != 0) {
+    if (!is_default_view(state->ipa_ctx->view_name)) {
 
         if ((state->ar->entry_type & BE_REQ_TYPE_MASK) == BE_REQ_GROUP
                 || ((state->ar->entry_type & BE_REQ_TYPE_MASK) == BE_REQ_BY_UUID
diff --git a/src/providers/ipa/ipa_s2n_exop.c b/src/providers/ipa/ipa_s2n_exop.c
index fa00691..812a4bb 100644
--- a/src/providers/ipa/ipa_s2n_exop.c
+++ b/src/providers/ipa/ipa_s2n_exop.c
@@ -1034,8 +1034,7 @@ static void ipa_s2n_get_fqlist_next(struct tevent_req *subreq)
         goto fail;
     }
 
-    if (state->ipa_ctx->view_name == NULL ||
-            strcmp(state->ipa_ctx->view_name, SYSDB_DEFAULT_VIEW_NAME) == 0) {
+    if (is_default_view(state->ipa_ctx->view_name)) {
         ret = ipa_s2n_get_fqlist_save_step(req);
         if (ret == EOK) {
             tevent_req_done(req);
@@ -1613,10 +1612,7 @@ static void ipa_s2n_get_user_done(struct tevent_req *subreq)
         ret = ENOENT;
     }
 
-    if (ret == ENOENT
-            || state->ipa_ctx->view_name == NULL
-            || strcmp(state->ipa_ctx->view_name,
-                      SYSDB_DEFAULT_VIEW_NAME) == 0) {
+    if (ret == ENOENT || is_default_view(state->ipa_ctx->view_name)) {
         ret = ipa_s2n_save_objects(state->dom, state->req_input, state->attrs,
                                    state->simple_attrs, NULL, NULL, true);
         if (ret != EOK) {
@@ -2167,7 +2163,7 @@ static errno_t ipa_s2n_save_objects(struct sss_domain_info *dom,
         goto done;
     }
 
-    if (view_name != NULL && strcmp(view_name, SYSDB_DEFAULT_VIEW_NAME) != 0) {
+    if (!is_default_view(view_name)) {
         /* For the default view the data return by the extdom plugin already
          * contains all needed data and it is not expected to have a separate
          * override object. */
@@ -2233,9 +2229,7 @@ static void ipa_s2n_get_fqlist_done(struct tevent_req  *subreq)
     }
 
     if (state->override_attrs == NULL
-            && state->ipa_ctx->view_name != NULL
-            && strcmp(state->ipa_ctx->view_name,
-                      SYSDB_DEFAULT_VIEW_NAME) != 0) {
+            && !is_default_view(state->ipa_ctx->view_name)) {
         subreq = ipa_get_ad_override_send(state, state->ev,
                            state->ipa_ctx->sdap_id_ctx,
                            state->ipa_ctx->ipa_options,
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index 0a5cdcc..cf72784 100644
--- a/src/providers/ipa/ipa_subdomains.c
+++ b/src/providers/ipa/ipa_subdomains.c
@@ -875,7 +875,7 @@ static void ipa_get_view_name_done(struct tevent_req *req)
     }
 
     DEBUG(SSSDBG_TRACE_FUNC, "Found view name [%s].\n", view_name);
-    if (strcmp(view_name, IPA_DEFAULT_VIEW_NAME) == 0) {
+    if (is_default_view(view_name)) {
         DEBUG(SSSDBG_TRACE_ALL,
               "Found IPA default view name, replacing with sysdb default.\n");
         view_name = SYSDB_DEFAULT_VIEW_NAME;
@@ -905,8 +905,7 @@ static void ipa_get_view_name_done(struct tevent_req *req)
                     goto done;
                 }
 
-                if (strcmp(ctx->sd_ctx->id_ctx->view_name,
-                           SYSDB_DEFAULT_VIEW_NAME) != 0) {
+                if (!is_default_view(ctx->sd_ctx->id_ctx->view_name)) {
                     /* Old view was not the default view, delete view tree */
                     ret = sysdb_delete_view_tree(
                                              ctx->sd_ctx->be_ctx->domain->sysdb,
diff --git a/src/providers/ipa/ipa_views.c b/src/providers/ipa/ipa_views.c
index ccaea69..331e118 100644
--- a/src/providers/ipa/ipa_views.c
+++ b/src/providers/ipa/ipa_views.c
@@ -277,7 +277,7 @@ struct tevent_req *ipa_get_ad_override_send(TALLOC_CTX *mem_ctx,
         goto done;
     }
 
-    if (strcmp(view_name, SYSDB_DEFAULT_VIEW_NAME) == 0) {
+    if (is_default_view(view_name)) {
         state->ipa_view_name = IPA_DEFAULT_VIEW_NAME;
     } else {
         state->ipa_view_name = view_name;
-- 
2.1.0

