summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/AuthInfo.java
blob: d2e5d49dffbf8d7e2f6be986597c380e03edf444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.client.info;

import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.extensions.client.AccountFieldName;
import com.google.gerrit.extensions.client.AuthType;
import com.google.gerrit.extensions.client.GitBasicAuthPolicy;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayString;
import java.util.ArrayList;
import java.util.List;

public class AuthInfo extends JavaScriptObject {
  public final AuthType authType() {
    return AuthType.valueOf(authTypeRaw());
  }

  public final boolean isLdap() {
    return authType() == AuthType.LDAP || authType() == AuthType.LDAP_BIND;
  }

  public final boolean isOpenId() {
    return authType() == AuthType.OPENID;
  }

  public final boolean isOAuth() {
    return authType() == AuthType.OAUTH;
  }

  public final boolean isDev() {
    return authType() == AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT;
  }

  public final boolean isClientSslCertLdap() {
    return authType() == AuthType.CLIENT_SSL_CERT_LDAP;
  }

  public final boolean isCustomExtension() {
    return authType() == AuthType.CUSTOM_EXTENSION;
  }

  public final boolean canEdit(AccountFieldName f) {
    return editableAccountFields().contains(f);
  }

  public final List<AccountFieldName> editableAccountFields() {
    List<AccountFieldName> fields = new ArrayList<>();
    for (String f : Natives.asList(_editableAccountFields())) {
      fields.add(AccountFieldName.valueOf(f));
    }
    return fields;
  }

  public final List<AgreementInfo> contributorAgreements() {
    List<AgreementInfo> agreements = new ArrayList<>();
    JsArray<AgreementInfo> contributorAgreements = _contributorAgreements();
    if (contributorAgreements != null) {
      for (AgreementInfo a : Natives.asList(contributorAgreements)) {
        agreements.add(a);
      }
    }
    return agreements;
  }

  public final boolean siteHasUsernames() {
    if (isCustomExtension() && httpPasswordUrl() != null && !canEdit(AccountFieldName.USER_NAME)) {
      return false;
    }
    return true;
  }

  public final boolean isHttpPasswordSettingsEnabled() {
    return gitBasicAuthPolicy() == GitBasicAuthPolicy.HTTP
        || gitBasicAuthPolicy() == GitBasicAuthPolicy.HTTP_LDAP;
  }

  public final GitBasicAuthPolicy gitBasicAuthPolicy() {
    return GitBasicAuthPolicy.valueOf(gitBasicAuthPolicyRaw());
  }

  public final native boolean useContributorAgreements()
      /*-{ return this.use_contributor_agreements || false; }-*/ ;

  public final native String loginUrl() /*-{ return this.login_url; }-*/;

  public final native String loginText() /*-{ return this.login_text; }-*/;

  public final native String switchAccountUrl() /*-{ return this.switch_account_url; }-*/;

  public final native String registerUrl() /*-{ return this.register_url; }-*/;

  public final native String registerText() /*-{ return this.register_text; }-*/;

  public final native String editFullNameUrl() /*-{ return this.edit_full_name_url; }-*/;

  public final native String httpPasswordUrl() /*-{ return this.http_password_url; }-*/;

  private native String gitBasicAuthPolicyRaw() /*-{ return this.git_basic_auth_policy; }-*/;

  private native String authTypeRaw() /*-{ return this.auth_type; }-*/;

  private native JsArrayString _editableAccountFields()
      /*-{ return this.editable_account_fields; }-*/ ;

  private native JsArray<AgreementInfo> _contributorAgreements()
      /*-{ return this.contributor_agreements; }-*/ ;

  protected AuthInfo() {}
}