Exemple #1
0
		private ServerModel(string url)
		{
			Url = url;
			Databases = new BindableCollection<string>(name => name);
			RecentDocuments = new Dictionary<string, QueueModel<string>>();
			SelectedDatabase = new Observable<DatabaseModel>();
			License = new Observable<LicensingStatus>();
			IsConnected = new Observable<bool> { Value = true };
			UserInfo = new UserInfo();
			Initialize();
		}
        protected UserInfo GetUserInfo()
        {
            var principal = User;
            var anonymousUserAccessMode = DatabasesLandlord.SystemConfiguration.AnonymousUserAccessMode;
            if (principal == null || principal.Identity == null || principal.Identity.IsAuthenticated == false)
            {
                var anonymous = new UserInfo
                {
                    Remark = "Using anonymous user",
                    IsAdminGlobal = anonymousUserAccessMode == AnonymousUserAccessMode.Admin,
                    IsAdminCurrentDb = anonymousUserAccessMode == AnonymousUserAccessMode.Admin ||
                                       anonymousUserAccessMode == AnonymousUserAccessMode.All
                };

                return anonymous;
            }

            var windowsPrincipal = principal as WindowsPrincipal;
            if (windowsPrincipal != null)
            {
                var windowsUser = new UserInfo
                {
                    Remark = "Using windows auth",
                    User = windowsPrincipal.Identity.Name,
                    IsAdminGlobal = windowsPrincipal.IsAdministrator("<system>") ||
                                    windowsPrincipal.IsAdministrator(anonymousUserAccessMode)
                };

                windowsUser.IsAdminCurrentDb = windowsUser.IsAdminGlobal || windowsPrincipal.IsAdministrator(Database);

                return windowsUser;
            }

            var principalWithDatabaseAccess = principal as PrincipalWithDatabaseAccess;
            if (principalWithDatabaseAccess != null)
            {
                var windowsUserWithDatabase = new UserInfo
                {
                    Remark = "Using windows auth",
                    User = principalWithDatabaseAccess.Identity.Name,
                    IsAdminGlobal = principalWithDatabaseAccess.IsAdministrator("<system>") ||
                                    principalWithDatabaseAccess.IsAdministrator(anonymousUserAccessMode),
                    IsAdminCurrentDb = principalWithDatabaseAccess.IsAdministrator(Database),
                    Databases =
                        principalWithDatabaseAccess.AdminDatabases.Concat(
                            principalWithDatabaseAccess.ReadOnlyDatabases)
                                                   .Concat(principalWithDatabaseAccess.ReadWriteDatabases)
                                                   .Select(db => new DatabaseInfo
                                                   {
                                                       Database = db,
                                                       IsAdmin = principal.IsAdministrator(db)
                                                   }).ToList(),
                    AdminDatabases = principalWithDatabaseAccess.AdminDatabases,
                    ReadOnlyDatabases = principalWithDatabaseAccess.ReadOnlyDatabases,
                    ReadWriteDatabases = principalWithDatabaseAccess.ReadWriteDatabases
                };

                windowsUserWithDatabase.IsAdminCurrentDb = windowsUserWithDatabase.IsAdminGlobal || principalWithDatabaseAccess.IsAdministrator(Database);

                return windowsUserWithDatabase;
            }

            var oAuthPrincipal = principal as OAuthPrincipal;
            if (oAuthPrincipal != null)
            {
                var oAuth = new UserInfo
                {
                    Remark = "Using OAuth",
                    User = oAuthPrincipal.Name,
                    IsAdminGlobal = oAuthPrincipal.IsAdministrator(anonymousUserAccessMode),
                    IsAdminCurrentDb = oAuthPrincipal.IsAdministrator(Database),
                    Databases = oAuthPrincipal.TokenBody.AuthorizedDatabases
                                              .Select(db => new DatabaseInfo
                                              {
                                                  Database = db.TenantId,
                                                  IsAdmin = principal.IsAdministrator(db.TenantId)
                                              }).ToList(),
                    AccessTokenBody = oAuthPrincipal.TokenBody,
                };

                return oAuth;
            }

            var unknown = new UserInfo
            {
                Remark = "Unknown auth",
                Principal = principal
            };

            return unknown;
        }
Exemple #3
0
		public override void Respond(IHttpContext context)
		{
			var principal = context.User;
			if (principal == null || principal.Identity == null || principal.Identity.IsAuthenticated == false)
			{
				var anonymous = new UserInfo
				{
					Remark = "Using anonymous user",
					IsAdminGlobal = server.SystemConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.Admin
				};
				context.WriteJson(RavenJObject.FromObject(anonymous));
				return;
			}
			var windowsPrincipal = principal as WindowsPrincipal;
			if (windowsPrincipal != null)
			{
				var windowsUser = new UserInfo
				{
					Remark = "Using windows auth",
					User = windowsPrincipal.Identity.Name,
					IsAdminGlobal = windowsPrincipal.IsAdministrator(server.SystemConfiguration.AnonymousUserAccessMode)
				};
				context.WriteJson(RavenJObject.FromObject(windowsUser));
				return;
			}

			var principalWithDatabaseAccess = principal as PrincipalWithDatabaseAccess;
			if (principalWithDatabaseAccess != null)
			{
				var databases = principalWithDatabaseAccess.AdminDatabases
					.Concat(principalWithDatabaseAccess.ReadOnlyDatabases)
					.Concat(principalWithDatabaseAccess.ReadWriteDatabases)
					.Select(db => new DatabaseInfo
					{
						Database = db,
						IsAdmin = principal.IsAdministrator(db)
					}).ToList();

				var windowsUserWithDatabase = new UserInfo
				{
					Remark = "Using windows auth",
					User = principalWithDatabaseAccess.Identity.Name,
					IsAdminGlobal = principalWithDatabaseAccess.IsAdministrator(server.SystemConfiguration.AnonymousUserAccessMode) ||
					                IsLocalGlobalAdmin(databases),
					IsAdminCurrentDb = principalWithDatabaseAccess.IsAdministrator(Database),
					Databases = databases,
					AdminDatabases = principalWithDatabaseAccess.AdminDatabases,
					ReadOnlyDatabases = principalWithDatabaseAccess.ReadOnlyDatabases,
					ReadWriteDatabases = principalWithDatabaseAccess.ReadWriteDatabases
				};

				context.WriteJson(RavenJObject.FromObject(windowsUserWithDatabase));
				return;
			}

			var oAuthPrincipal = principal as OAuthPrincipal;
			if (oAuthPrincipal != null)
			{
				var databases = oAuthPrincipal.TokenBody.AuthorizedDatabases
					                                      .Select(db => new DatabaseInfo
					                                                    {
						                                                    Database = db.TenantId,
						                                                    IsAdmin = principal.IsAdministrator(db.TenantId)
					                                                    }).ToList();
				var oAuth = new UserInfo
				            {
					            Remark = "Using OAuth",
					            User = oAuthPrincipal.Name,
					            IsAdminGlobal = oAuthPrincipal.IsAdministrator(server.SystemConfiguration.AnonymousUserAccessMode) || IsLocalGlobalAdmin(databases),
					            IsAdminCurrentDb = oAuthPrincipal.IsAdministrator(Database),
					            Databases = databases,
					            AccessTokenBody = oAuthPrincipal.TokenBody,
				            };
				context.WriteJson(RavenJObject.FromObject(oAuth));
				return;
			}

			var unknown = new UserInfo
			              {
				              Remark = "Unknown auth",
				              Principal = principal
			              };
			context.WriteJson(RavenJObject.FromObject(unknown));
		}
		public HttpResponseMessage UserInfo()
		{
			var principal = User;
			if (principal == null || principal.Identity == null || principal.Identity.IsAuthenticated == false)
			{
				var anonymous = new UserInfo
				{
					Remark = "Using anonymous user",
					IsAdminGlobal = DatabasesLandlord.SystemConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.Admin
				};
				return GetMessageWithObject(anonymous);
			}

			var windowsPrincipal = principal as WindowsPrincipal;
			if (windowsPrincipal != null)
			{
				var windowsUser = new UserInfo
				{
					Remark = "Using windows auth",
					User = windowsPrincipal.Identity.Name,
					IsAdminGlobal =
						windowsPrincipal.IsAdministrator(DatabasesLandlord.SystemConfiguration.AnonymousUserAccessMode)
				};

				return GetMessageWithObject(windowsUser);
			}

			var principalWithDatabaseAccess = principal as PrincipalWithDatabaseAccess;
			if (principalWithDatabaseAccess != null)
			{
				var windowsUserWithDatabase = new UserInfo
				{
					Remark = "Using windows auth",
					User = principalWithDatabaseAccess.Identity.Name,
					IsAdminGlobal =
						principalWithDatabaseAccess.IsAdministrator(
							DatabasesLandlord.SystemConfiguration.AnonymousUserAccessMode),
					IsAdminCurrentDb = principalWithDatabaseAccess.IsAdministrator(Database),
					Databases =
						principalWithDatabaseAccess.AdminDatabases.Concat(
							principalWithDatabaseAccess.ReadOnlyDatabases)
												   .Concat(principalWithDatabaseAccess.ReadWriteDatabases)
												   .Select(db => new DatabaseInfo
												   {
													   Database = db,
													   IsAdmin = principal.IsAdministrator(db)
												   }).ToList(),
					AdminDatabases = principalWithDatabaseAccess.AdminDatabases,
					ReadOnlyDatabases = principalWithDatabaseAccess.ReadOnlyDatabases,
					ReadWriteDatabases = principalWithDatabaseAccess.ReadWriteDatabases
				};

				return GetMessageWithObject(windowsUserWithDatabase);
			}

			var oAuthPrincipal = principal as OAuthPrincipal;
			if (oAuthPrincipal != null)
			{
				var oAuth = new UserInfo
				{
					Remark = "Using OAuth",
					User = oAuthPrincipal.Name,
					IsAdminGlobal = oAuthPrincipal.IsAdministrator(DatabasesLandlord.SystemConfiguration.AnonymousUserAccessMode),
					IsAdminCurrentDb = oAuthPrincipal.IsAdministrator(Database),
					Databases = oAuthPrincipal.TokenBody.AuthorizedDatabases
											  .Select(db => new DatabaseInfo
											  {
												  Database = db.TenantId,
												  IsAdmin = principal.IsAdministrator(db.TenantId)
											  }).ToList(),
					AccessTokenBody = oAuthPrincipal.TokenBody,
				};

				return GetMessageWithObject(oAuth);
			}

			var unknown = new UserInfo
			{
				Remark = "Unknown auth",
				Principal = principal
			};

			return GetMessageWithObject(unknown);
		}