コード例 #1
0
ファイル: WebUserTep.cs プロジェクト: Terradue/DotNetTep
        /// <summary>
        /// Initializes a new instance of the <see cref="Terradue.Tep.WebServer.WebUserTep"/> class.
        /// </summary>
        /// <param name="entity">Entity.</param>
        public WebUserTep(IfyWebContext context, UserTep entity, bool umsso = false) : base(entity)
        {
            if (umsso)
            {
                AuthenticationType umssoauthType = IfyWebContext.GetAuthenticationType(typeof(UmssoAuthenticationType));
                var umssoUser = umssoauthType.GetUserProfile(context, HttpContext.Current.Request, false);
                if (umssoUser != null)
                {
                    this.UmssoEmail = umssoUser.Email;
                }
            }

            //only current user can know the api key
            if (context.UserId == entity.Id)
            {
                this.ApiKey         = entity.ApiKey;
                this.T2ProfileError = HttpContext.Current.Session["t2profileError"] as string;
                if ((string.IsNullOrEmpty(entity.Affiliation) || string.IsNullOrEmpty(entity.Country) || string.IsNullOrEmpty(entity.FirstName) || string.IsNullOrEmpty(entity.LastName)))
                {
                    this.T2ProfileError += (string.IsNullOrEmpty(this.T2ProfileError) ? "" : "\n") + "Profile not complete";
                }
                this.T2ApiKey = entity.GetSessionApiKey();
            }

            if (context.UserId == entity.Id || context.UserLevel == UserLevel.Administrator)
            {
                this.T2Username = entity.TerradueCloudUsername;
                if (context.GetConfigBooleanValue("accounting-enabled"))
                {
                    this.Balance = entity.GetAccountingBalance();
                }
                this.Roles = GetUserCommunityRoles(context, entity);
                if (context.UserLevel == UserLevel.Administrator)
                {
                    if (entity.RegistrationDate == DateTime.MinValue)
                    {
                        entity.LoadRegistrationInfo();
                    }
                    this.RegistrationDate = entity.RegistrationDate;
                }
            }
            else
            {
                this.Email         = null;
                this.Affiliation   = null;
                this.Level         = 0;
                this.AccountStatus = 0;
                this.DomainId      = null;
            }
        }
コード例 #2
0
        public object Get(ThematicAppCurrentUserSearchRequestTep request)
        {
            IfyWebContext context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);

            context.Open();
            context.LogInfo(this, string.Format("/user/current/apps/search GET"));

            IOpenSearchResultCollection result;
            OpenSearchEngine            ose = MasterCatalogue.OpenSearchEngine;
            var  httpRequest  = HttpContext.Current.Request;
            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            List <Terradue.OpenSearch.IOpenSearchable> osentities = new List <Terradue.OpenSearch.IOpenSearchable>();
            var settings = MasterCatalogue.OpenSearchFactorySettings;
            OpenSearchableFactorySettings specsettings = (OpenSearchableFactorySettings)settings.Clone();

            UserTep user = null;

            if (context.UserId != 0)
            {
                user = UserTep.FromId(context, context.UserId);
                if (request.cache)
                {
                    var domain = user.GetPrivateDomain();
                    EntityList <ThematicApplicationCached> appsCached = new EntityList <ThematicApplicationCached>(context);
                    appsCached.SetFilter("DomainId", domain.Id);
                    appsCached.AddSort("LastUpdate", SortDirection.Descending);

                    result = ose.Query(appsCached, Request.QueryString, responseType);
                    OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(appsCached, result);
                }
                else
                {
                    //get user private thematic app
                    var apikey   = user.GetSessionApiKey();
                    var t2userid = user.TerradueCloudUsername;
                    if (!string.IsNullOrEmpty(apikey))
                    {
                        specsettings.Credentials = new System.Net.NetworkCredential(t2userid, apikey);
                    }
                    var app = user.GetPrivateThematicApp();
                    if (app != null)
                    {
                        foreach (var item in app.Items)
                        {
                            if (!string.IsNullOrEmpty(item.Location))
                            {
                                try {
                                    var sgOs = OpenSearchFactory.FindOpenSearchable(specsettings, new OpenSearchUrl(item.Location));
                                    osentities.Add(sgOs);
                                    context.LogDebug(this, string.Format("Apps search -- Add '{0}'", item.Location));
                                } catch (Exception e) {
                                    context.LogError(this, e.Message, e);
                                }
                            }
                        }
                    }
                    MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, specsettings);
                    result = ose.Query(multiOSE, Request.QueryString, responseType);
                }
            }
            else
            {
                result = ose.Query(new MultiGenericOpenSearchable(osentities, specsettings), Request.QueryString, responseType);
            }

            string sresult = result.SerializeToString();

            //replace usernames in apps
            if (user != null)
            {
                try {
                    sresult = sresult.Replace("${USERNAME}", user.Username);
                    sresult = sresult.Replace("${T2USERNAME}", user.TerradueCloudUsername);
                    sresult = sresult.Replace("${T2APIKEY}", user.GetSessionApiKey());
                } catch (Exception e) {
                    context.LogError(this, e.Message, e);
                }
            }

            context.Close();

            return(new HttpResult(sresult, result.ContentType));
        }