-
-
Notifications
You must be signed in to change notification settings - Fork 780
Expand file tree
/
Copy pathStandardTabAndModuleInfoProvider.cs
More file actions
172 lines (146 loc) · 5.73 KB
/
Copy pathStandardTabAndModuleInfoProvider.cs
File metadata and controls
172 lines (146 loc) · 5.73 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.Web.Mvc
{
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Instrumentation;
public sealed class StandardTabAndModuleInfoProvider : ITabAndModuleInfoProvider
{
private const string ModuleIdKey = "ModuleId";
private const string TabIdKey = "TabId";
private const string MonikerQueryKey = "Moniker";
private const string MonikerHeaderKey = "X-DNN-MONIKER";
private const string MonikerSettingsKey = "Moniker";
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(StandardTabAndModuleInfoProvider));
/// <inheritdoc />
public bool TryFindTabId(HttpRequestBase request, out int tabId)
{
return TryFindTabId(request, out tabId, true);
}
/// <inheritdoc />
public bool TryFindModuleId(HttpRequestBase request, out int moduleId)
{
return TryFindModuleId(request, out moduleId, true);
}
/// <inheritdoc />
public bool TryFindModuleInfo(HttpRequestBase request, out ModuleInfo moduleInfo)
{
int tabId, moduleId;
if (TryFindTabId(request, out tabId, false) && TryFindModuleId(request, out moduleId, false))
{
moduleInfo = ModuleController.Instance.GetModule(moduleId, tabId, false);
return moduleInfo != null;
}
return TryFindByMoniker(request, out moduleInfo);
}
private static bool TryFindTabId(HttpRequestBase request, out int tabId, bool tryMoniker)
{
tabId = FindInt(request, TabIdKey);
if (tabId > Null.NullInteger)
{
return true;
}
if (tryMoniker)
{
ModuleInfo moduleInfo;
if (TryFindByMoniker(request, out moduleInfo))
{
tabId = moduleInfo.TabID;
return true;
}
}
return false;
}
private static bool TryFindModuleId(HttpRequestBase request, out int moduleId, bool tryMoniker)
{
moduleId = FindInt(request, ModuleIdKey);
if (moduleId > Null.NullInteger)
{
return true;
}
if (tryMoniker)
{
ModuleInfo moduleInfo;
if (TryFindByMoniker(request, out moduleInfo))
{
moduleId = moduleInfo.ModuleID;
return true;
}
}
return false;
}
private static int FindInt(HttpRequestBase requestBase, string key)
{
string value = null;
if (requestBase.Headers[key] != null)
{
value = requestBase.Headers[key];
}
if (requestBase.Form[key] != null)
{
value = requestBase.Form[key];
}
if (string.IsNullOrEmpty(value) && requestBase.Url != null)
{
var queryString = HttpUtility.ParseQueryString(requestBase.Url.Query);
value = queryString[key];
}
int id;
return int.TryParse(value, out id) ? id : Null.NullInteger;
}
private static bool TryFindByMoniker(HttpRequestBase requestBase, out ModuleInfo moduleInfo)
{
var id = FindIntInHeader(requestBase, MonikerHeaderKey);
if (id <= Null.NullInteger)
{
id = FindIntInQueryString(requestBase, MonikerQueryKey);
}
moduleInfo = id > Null.NullInteger ? ModuleController.Instance.GetTabModule(id) : null;
return moduleInfo != null;
}
private static int FindIntInHeader(HttpRequestBase requestBase, string key)
{
string value = null;
if (requestBase.Headers[key] != null)
{
value = requestBase.Headers[key];
}
return GetTabModuleInfoFromMoniker(value);
}
private static int FindIntInQueryString(HttpRequestBase requestBase, string key)
{
string value = null;
if (requestBase.Url != null)
{
var queryString = HttpUtility.ParseQueryString(requestBase.Url.Query);
value = queryString[key];
}
return GetTabModuleInfoFromMoniker(value);
}
private static int GetTabModuleInfoFromMoniker(string monikerValue)
{
monikerValue = (monikerValue ?? string.Empty).Trim();
if (monikerValue.Length > 0)
{
var ids = TabModulesController.Instance.GetTabModuleIdsBySetting(MonikerSettingsKey, monikerValue);
if (ids != null && ids.Any())
{
return ids.First();
}
if (Logger.IsWarnEnabled)
{
Logger.WarnFormat(CultureInfo.InvariantCulture, "The specified moniker ({0}) is not defined in the system", monikerValue);
}
}
return Null.NullInteger;
}
}
}