authz-controller.js 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035
  1. module.controller('ResourceServerCtrl', function($scope, realm, ResourceServer) {
  2. $scope.realm = realm;
  3. ResourceServer.query({realm : realm.realm}, function (data) {
  4. $scope.servers = data;
  5. });
  6. });
  7. module.controller('ResourceServerDetailCtrl', function($scope, $http, $route, $location, $upload, $modal, realm, ResourceServer, client, AuthzDialog, Notifications) {
  8. $scope.realm = realm;
  9. $scope.client = client;
  10. ResourceServer.get({
  11. realm : $route.current.params.realm,
  12. client : client.id
  13. }, function(data) {
  14. $scope.server = angular.copy(data);
  15. $scope.changed = false;
  16. $scope.$watch('server', function() {
  17. if (!angular.equals($scope.server, data)) {
  18. $scope.changed = true;
  19. }
  20. }, true);
  21. $scope.save = function() {
  22. ResourceServer.update({realm : realm.realm, client : $scope.server.clientId}, $scope.server, function() {
  23. $route.reload();
  24. Notifications.success("The resource server has been created.");
  25. });
  26. }
  27. $scope.reset = function() {
  28. $route.reload();
  29. }
  30. $scope.export = function() {
  31. $scope.exportSettings = true;
  32. ResourceServer.settings({
  33. realm : $route.current.params.realm,
  34. client : client.id
  35. }, function(data) {
  36. var tmp = angular.fromJson(data);
  37. $scope.settings = angular.toJson(tmp, true);
  38. })
  39. }
  40. $scope.downloadSettings = function() {
  41. saveAs(new Blob([$scope.settings], { type: 'application/json' }), $scope.server.name + "-authz-config.json");
  42. }
  43. $scope.cancelExport = function() {
  44. delete $scope.settings
  45. }
  46. $scope.onFileSelect = function($fileContent) {
  47. $scope.server = angular.copy(JSON.parse($fileContent));
  48. $scope.importing = true;
  49. };
  50. $scope.viewImportDetails = function() {
  51. $modal.open({
  52. templateUrl: resourceUrl + '/partials/modal/view-object.html',
  53. controller: 'ObjectModalCtrl',
  54. resolve: {
  55. object: function () {
  56. return $scope.server;
  57. }
  58. }
  59. })
  60. };
  61. $scope.import = function () {
  62. ResourceServer.import({realm : realm.realm, client : client.id}, $scope.server, function() {
  63. $route.reload();
  64. Notifications.success("The resource server has been updated.");
  65. });
  66. }
  67. });
  68. });
  69. var Resources = {
  70. delete: function(ResourceServerResource, realm, client, $scope, AuthzDialog, $location, Notifications, $route) {
  71. ResourceServerResource.permissions({
  72. realm : realm,
  73. client : client.id,
  74. rsrid : $scope.resource._id
  75. }, function (permissions) {
  76. var msg = "";
  77. if (permissions.length > 0 && !$scope.deleteConsent) {
  78. msg = "<p>This resource is referenced in some permissions:</p>";
  79. msg += "<ul>";
  80. for (i = 0; i < permissions.length; i++) {
  81. msg+= "<li><strong>" + permissions[i].name + "</strong></li>";
  82. }
  83. msg += "</ul>";
  84. msg += "<p>If you remove this resource, the permissions above will be affected and will not be associated with this resource anymore.</p>";
  85. }
  86. AuthzDialog.confirmDeleteWithMsg($scope.resource.name, "Resource", msg, function() {
  87. ResourceServerResource.delete({realm : realm, client : $scope.client.id, rsrid : $scope.resource._id}, null, function() {
  88. $location.url("/realms/" + realm + "/clients/" + $scope.client.id + "/authz/resource-server/resource");
  89. $route.reload();
  90. Notifications.success("The resource has been deleted.");
  91. });
  92. });
  93. });
  94. }
  95. }
  96. var Policies = {
  97. delete: function(service, realm, client, $scope, AuthzDialog, $location, Notifications, $route, isPermission) {
  98. var msg = "";
  99. service.dependentPolicies({
  100. realm : realm,
  101. client : client.id,
  102. id : $scope.policy.id
  103. }, function (dependentPolicies) {
  104. if (dependentPolicies.length > 0 && !$scope.deleteConsent) {
  105. msg = "<p>This policy is being used by other policies:</p>";
  106. msg += "<ul>";
  107. for (i = 0; i < dependentPolicies.length; i++) {
  108. msg+= "<li><strong>" + dependentPolicies[i].name + "</strong></li>";
  109. }
  110. msg += "</ul>";
  111. msg += "<p>If you remove this policy, the policies above will be affected and will not be associated with this policy anymore.</p>";
  112. }
  113. AuthzDialog.confirmDeleteWithMsg($scope.policy.name, isPermission ? "Permission" : "Policy", msg, function() {
  114. service.delete({realm : realm, client : $scope.client.id, id : $scope.policy.id}, null, function() {
  115. if (isPermission) {
  116. $location.url("/realms/" + realm + "/clients/" + $scope.client.id + "/authz/resource-server/permission");
  117. Notifications.success("The permission has been deleted.");
  118. } else {
  119. $location.url("/realms/" + realm + "/clients/" + $scope.client.id + "/authz/resource-server/policy");
  120. Notifications.success("The policy has been deleted.");
  121. }
  122. $route.reload();
  123. });
  124. });
  125. });
  126. }
  127. }
  128. module.controller('ResourceServerResourceCtrl', function($scope, $http, $route, $location, realm, ResourceServer, ResourceServerResource, client, AuthzDialog, Notifications, viewState) {
  129. $scope.realm = realm;
  130. $scope.client = client;
  131. $scope.query = {
  132. realm: realm.realm,
  133. client : client.id,
  134. deep: false,
  135. max : 20,
  136. first : 0
  137. };
  138. $scope.listSizes = [5, 10, 20];
  139. ResourceServer.get({
  140. realm : $route.current.params.realm,
  141. client : client.id
  142. }, function(data) {
  143. $scope.server = data;
  144. $scope.createPolicy = function(resource) {
  145. viewState.state = {};
  146. viewState.state.previousUrl = '/realms/' + $route.current.params.realm + '/clients/' + client.id + '/authz/resource-server/resource';
  147. $location.path('/realms/' + $route.current.params.realm + '/clients/' + client.id + '/authz/resource-server/permission/resource/create').search({rsrid: resource._id});
  148. }
  149. $scope.searchQuery();
  150. });
  151. $scope.firstPage = function() {
  152. $scope.query.first = 0;
  153. $scope.searchQuery();
  154. }
  155. $scope.previousPage = function() {
  156. $scope.query.first -= parseInt($scope.query.max);
  157. if ($scope.query.first < 0) {
  158. $scope.query.first = 0;
  159. }
  160. $scope.searchQuery();
  161. }
  162. $scope.nextPage = function() {
  163. $scope.query.first += parseInt($scope.query.max);
  164. $scope.searchQuery();
  165. }
  166. $scope.searchQuery = function() {
  167. $scope.searchLoaded = false;
  168. ResourceServerResource.query($scope.query, function(response) {
  169. $scope.searchLoaded = true;
  170. $scope.lastSearch = $scope.query.search;
  171. $scope.resources = response;
  172. if ($scope.detailsFilter) {
  173. $scope.showDetails();
  174. }
  175. });
  176. };
  177. $scope.loadDetails = function (resource) {
  178. if (resource.details) {
  179. resource.details.loaded = !resource.details.loaded;
  180. return;
  181. }
  182. resource.details = {loaded: false};
  183. ResourceServerResource.scopes({
  184. realm : $route.current.params.realm,
  185. client : client.id,
  186. rsrid : resource._id
  187. }, function(response) {
  188. resource.scopes = response;
  189. ResourceServerResource.permissions({
  190. realm : $route.current.params.realm,
  191. client : client.id,
  192. rsrid : resource._id
  193. }, function(response) {
  194. resource.policies = response;
  195. resource.details.loaded = true;
  196. });
  197. });
  198. }
  199. $scope.showDetails = function(item, event) {
  200. if (event.target.localName == 'a' || event.target.localName == 'button') {
  201. return;
  202. }
  203. if (item) {
  204. $scope.loadDetails(item);
  205. } else {
  206. for (i = 0; i < $scope.resources.length; i++) {
  207. $scope.loadDetails($scope.resources[i]);
  208. }
  209. }
  210. };
  211. $scope.delete = function(resource) {
  212. $scope.resource = resource;
  213. Resources.delete(ResourceServerResource, $route.current.params.realm, client, $scope, AuthzDialog, $location, Notifications, $route);
  214. };
  215. });
  216. module.controller('ResourceServerResourceDetailCtrl', function($scope, $http, $route, $location, realm, ResourceServer, client, ResourceServerResource, ResourceServerScope, AuthzDialog, Notifications) {
  217. $scope.realm = realm;
  218. $scope.client = client;
  219. $scope.scopesUiSelect = {
  220. minimumInputLength: 1,
  221. delay: 500,
  222. allowClear: true,
  223. query: function (query) {
  224. var data = {results: []};
  225. if ('' == query.term.trim()) {
  226. query.callback(data);
  227. return;
  228. }
  229. $scope.query = {
  230. realm: realm.realm,
  231. client : client.id,
  232. name: query.term.trim(),
  233. deep: false,
  234. max : 20,
  235. first : 0
  236. };
  237. ResourceServerScope.query($scope.query, function(response) {
  238. data.results = response;
  239. query.callback(data);
  240. });
  241. },
  242. formatResult: function(object, container, query) {
  243. return object.name;
  244. },
  245. formatSelection: function(object, container, query) {
  246. return object.name;
  247. }
  248. };
  249. var $instance = this;
  250. ResourceServer.get({
  251. realm : $route.current.params.realm,
  252. client : client.id
  253. }, function(data) {
  254. $scope.server = data;
  255. var resourceId = $route.current.params.rsrid;
  256. if (!resourceId) {
  257. $scope.create = true;
  258. $scope.changed = false;
  259. var resource = {};
  260. resource.scopes = [];
  261. resource.attributes = {};
  262. resource.uris = [];
  263. $scope.resource = angular.copy(resource);
  264. $scope.$watch('resource', function() {
  265. if (!angular.equals($scope.resource, resource)) {
  266. $scope.changed = true;
  267. }
  268. }, true);
  269. $scope.$watch('newUri', function() {
  270. if ($scope.newUri && $scope.newUri.length > 0) {
  271. $scope.changed = true;
  272. }
  273. }, true);
  274. $scope.save = function() {
  275. if ($scope.newUri && $scope.newUri.length > 0) {
  276. $scope.addUri();
  277. }
  278. for (i = 0; i < $scope.resource.scopes.length; i++) {
  279. delete $scope.resource.scopes[i].text;
  280. }
  281. $instance.checkNameAvailability(function () {
  282. ResourceServerResource.save({realm : realm.realm, client : $scope.client.id}, $scope.resource, function(data) {
  283. $location.url("/realms/" + realm.realm + "/clients/" + $scope.client.id + "/authz/resource-server/resource/" + data._id);
  284. Notifications.success("The resource has been created.");
  285. });
  286. });
  287. }
  288. $scope.reset = function() {
  289. $location.url("/realms/" + realm.realm + "/clients/" + $scope.client.id + "/authz/resource-server/resource/");
  290. }
  291. } else {
  292. ResourceServerResource.get({
  293. realm : $route.current.params.realm,
  294. client : client.id,
  295. rsrid : $route.current.params.rsrid,
  296. }, function(data) {
  297. if (!data.scopes) {
  298. data.scopes = [];
  299. }
  300. if (!data.attributes) {
  301. data.attributes = {};
  302. }
  303. $scope.resource = angular.copy(data);
  304. $scope.changed = false;
  305. $scope.originalResource = angular.copy($scope.resource);
  306. $scope.$watch('resource', function() {
  307. if (!angular.equals($scope.resource, data)) {
  308. $scope.changed = true;
  309. }
  310. }, true);
  311. $scope.$watch('newUri', function() {
  312. if ($scope.newUri && $scope.newUri.length > 0) {
  313. $scope.changed = true;
  314. }
  315. }, true);
  316. $scope.save = function() {
  317. if ($scope.newUri && $scope.newUri.length > 0) {
  318. $scope.addUri();
  319. }
  320. for (i = 0; i < $scope.resource.scopes.length; i++) {
  321. delete $scope.resource.scopes[i].text;
  322. }
  323. var keys = Object.keys($scope.resource.attributes);
  324. for (var k = 0; k < keys.length; k++) {
  325. var key = keys[k];
  326. var value = $scope.resource.attributes[key];
  327. var values = value.toString().split(',');
  328. $scope.resource.attributes[key] = [];
  329. for (j = 0; j < values.length; j++) {
  330. $scope.resource.attributes[key].push(values[j]);
  331. }
  332. }
  333. $instance.checkNameAvailability(function () {
  334. ResourceServerResource.update({realm : realm.realm, client : $scope.client.id, rsrid : $scope.resource._id}, $scope.resource, function() {
  335. $route.reload();
  336. Notifications.success("The resource has been updated.");
  337. });
  338. });
  339. }
  340. $scope.remove = function() {
  341. Resources.delete(ResourceServerResource, $route.current.params.realm, client, $scope, AuthzDialog, $location, Notifications, $route);
  342. }
  343. $scope.reset = function() {
  344. $route.reload();
  345. }
  346. });
  347. }
  348. });
  349. $scope.checkNewNameAvailability = function () {
  350. $instance.checkNameAvailability(function () {});
  351. }
  352. this.checkNameAvailability = function (onSuccess) {
  353. if (!$scope.resource.name || $scope.resource.name.trim().length == 0) {
  354. return;
  355. }
  356. ResourceServerResource.search({
  357. realm : $route.current.params.realm,
  358. client : client.id,
  359. rsrid : $route.current.params.rsrid,
  360. name: $scope.resource.name
  361. }, function(data) {
  362. if (data && data._id && data._id != $scope.resource._id) {
  363. Notifications.error("Name already in use by another resource, please choose another one.");
  364. } else {
  365. onSuccess();
  366. }
  367. });
  368. }
  369. $scope.addAttribute = function() {
  370. $scope.resource.attributes[$scope.newAttribute.key] = $scope.newAttribute.value;
  371. delete $scope.newAttribute;
  372. }
  373. $scope.removeAttribute = function(key) {
  374. delete $scope.resource.attributes[key];
  375. }
  376. $scope.addUri = function() {
  377. $scope.resource.uris.push($scope.newUri);
  378. $scope.newUri = "";
  379. }
  380. $scope.deleteUri = function(index) {
  381. $scope.resource.uris.splice(index, 1);
  382. }
  383. });
  384. var Scopes = {
  385. delete: function(ResourceServerScope, realm, client, $scope, AuthzDialog, $location, Notifications, $route) {
  386. ResourceServerScope.permissions({
  387. realm : realm,
  388. client : client.id,
  389. id : $scope.scope.id
  390. }, function (permissions) {
  391. var msg = "";
  392. if (permissions.length > 0 && !$scope.deleteConsent) {
  393. msg = "<p>This scope is referenced in some permissions:</p>";
  394. msg += "<ul>";
  395. for (i = 0; i < permissions.length; i++) {
  396. msg+= "<li><strong>" + permissions[i].name + "</strong></li>";
  397. }
  398. msg += "</ul>";
  399. msg += "<p>If you remove this scope, the permissions above will be affected and will not be associated with this scope anymore.</p>";
  400. }
  401. AuthzDialog.confirmDeleteWithMsg($scope.scope.name, "Scope", msg, function() {
  402. ResourceServerScope.delete({realm : realm, client : $scope.client.id, id : $scope.scope.id}, null, function() {
  403. $location.url("/realms/" + realm + "/clients/" + $scope.client.id + "/authz/resource-server/scope");
  404. $route.reload();
  405. Notifications.success("The scope has been deleted.");
  406. });
  407. });
  408. });
  409. }
  410. }
  411. module.controller('ResourceServerScopeCtrl', function($scope, $http, $route, $location, realm, ResourceServer, ResourceServerScope,client, AuthzDialog, Notifications, viewState) {
  412. $scope.realm = realm;
  413. $scope.client = client;
  414. $scope.query = {
  415. realm: realm.realm,
  416. client : client.id,
  417. deep: false,
  418. max : 20,
  419. first : 0
  420. };
  421. $scope.listSizes = [5, 10, 20];
  422. ResourceServer.get({
  423. realm : $route.current.params.realm,
  424. client : client.id
  425. }, function(data) {
  426. $scope.server = data;
  427. $scope.createPolicy = function(scope) {
  428. viewState.state = {};
  429. viewState.state.previousUrl = '/realms/' + $route.current.params.realm + '/clients/' + client.id + '/authz/resource-server/scope';
  430. $location.path('/realms/' + $route.current.params.realm + '/clients/' + client.id + '/authz/resource-server/permission/scope/create').search({scpid: scope.id});
  431. }
  432. $scope.searchQuery();
  433. });
  434. $scope.firstPage = function() {
  435. $scope.query.first = 0;
  436. $scope.searchQuery();
  437. }
  438. $scope.previousPage = function() {
  439. $scope.query.first -= parseInt($scope.query.max);
  440. if ($scope.query.first < 0) {
  441. $scope.query.first = 0;
  442. }
  443. $scope.searchQuery();
  444. }
  445. $scope.nextPage = function() {
  446. $scope.query.first += parseInt($scope.query.max);
  447. $scope.searchQuery();
  448. }
  449. $scope.searchQuery = function(detailsFilter) {
  450. $scope.searchLoaded = false;
  451. ResourceServerScope.query($scope.query, function(response) {
  452. $scope.scopes = response;
  453. $scope.searchLoaded = true;
  454. $scope.lastSearch = $scope.query.search;
  455. if ($scope.detailsFilter) {
  456. $scope.showDetails();
  457. }
  458. });
  459. };
  460. $scope.loadDetails = function (scope) {
  461. if (scope.details) {
  462. scope.details.loaded = !scope.details.loaded;
  463. return;
  464. }
  465. scope.details = {loaded: false};
  466. ResourceServerScope.resources({
  467. realm : $route.current.params.realm,
  468. client : client.id,
  469. id : scope.id
  470. }, function(response) {
  471. scope.resources = response;
  472. ResourceServerScope.permissions({
  473. realm : $route.current.params.realm,
  474. client : client.id,
  475. id : scope.id
  476. }, function(response) {
  477. scope.policies = response;
  478. scope.details.loaded = true;
  479. });
  480. });
  481. }
  482. $scope.showDetails = function(item, event) {
  483. if (event.target.localName == 'a' || event.target.localName == 'button') {
  484. return;
  485. }
  486. if (item) {
  487. $scope.loadDetails(item);
  488. } else {
  489. for (i = 0; i < $scope.scopes.length; i++) {
  490. $scope.loadDetails($scope.scopes[i]);
  491. }
  492. }
  493. };
  494. $scope.delete = function(scope) {
  495. $scope.scope = scope;
  496. Scopes.delete(ResourceServerScope, $route.current.params.realm, client, $scope, AuthzDialog, $location, Notifications, $route);
  497. };
  498. });
  499. module.controller('ResourceServerScopeDetailCtrl', function($scope, $http, $route, $location, realm, ResourceServer, client, ResourceServerScope, AuthzDialog, Notifications) {
  500. $scope.realm = realm;
  501. $scope.client = client;
  502. var $instance = this;
  503. ResourceServer.get({
  504. realm : $route.current.params.realm,
  505. client : client.id
  506. }, function(data) {
  507. $scope.server = data;
  508. var scopeId = $route.current.params.id;
  509. if (!scopeId) {
  510. $scope.create = true;
  511. $scope.changed = false;
  512. var scope = {};
  513. $scope.scope = angular.copy(scope);
  514. $scope.$watch('scope', function() {
  515. if (!angular.equals($scope.scope, scope)) {
  516. $scope.changed = true;
  517. }
  518. }, true);
  519. $scope.save = function() {
  520. $instance.checkNameAvailability(function () {
  521. ResourceServerScope.save({realm : realm.realm, client : $scope.client.id}, $scope.scope, function(data) {
  522. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/scope/" + data.id);
  523. Notifications.success("The scope has been created.");
  524. });
  525. });
  526. }
  527. $scope.reset = function() {
  528. $location.url("/realms/" + realm.realm + "/clients/" + $scope.client.id + "/authz/resource-server/scope/");
  529. }
  530. } else {
  531. ResourceServerScope.get({
  532. realm : $route.current.params.realm,
  533. client : client.id,
  534. id : $route.current.params.id,
  535. }, function(data) {
  536. $scope.scope = angular.copy(data);
  537. $scope.changed = false;
  538. $scope.$watch('scope', function() {
  539. if (!angular.equals($scope.scope, data)) {
  540. $scope.changed = true;
  541. }
  542. }, true);
  543. $scope.originalScope = angular.copy($scope.scope);
  544. $scope.save = function() {
  545. $instance.checkNameAvailability(function () {
  546. ResourceServerScope.update({realm : realm.realm, client : $scope.client.id, id : $scope.scope.id}, $scope.scope, function() {
  547. $scope.changed = false;
  548. Notifications.success("The scope has been updated.");
  549. });
  550. });
  551. }
  552. $scope.remove = function() {
  553. Scopes.delete(ResourceServerScope, $route.current.params.realm, client, $scope, AuthzDialog, $location, Notifications, $route);
  554. }
  555. $scope.reset = function() {
  556. $route.reload();
  557. }
  558. });
  559. }
  560. });
  561. $scope.checkNewNameAvailability = function () {
  562. $instance.checkNameAvailability(function () {});
  563. }
  564. this.checkNameAvailability = function (onSuccess) {
  565. if (!$scope.scope.name || $scope.scope.name.trim().length == 0) {
  566. return;
  567. }
  568. ResourceServerScope.search({
  569. realm : $route.current.params.realm,
  570. client : client.id,
  571. name: $scope.scope.name
  572. }, function(data) {
  573. if (data && data.id && data.id != $scope.scope.id) {
  574. Notifications.error("Name already in use by another scope, please choose another one.");
  575. } else {
  576. onSuccess();
  577. }
  578. });
  579. }
  580. });
  581. module.controller('ResourceServerPolicyCtrl', function($scope, $http, $route, $location, realm, ResourceServer, ResourceServerPolicy, PolicyProvider, client, AuthzDialog, Notifications, KcStrings) {
  582. $scope.realm = realm;
  583. $scope.client = client;
  584. $scope.policyProviders = [];
  585. $scope.query = {
  586. realm: realm.realm,
  587. client : client.id,
  588. permission: false,
  589. max: 20,
  590. first : 0
  591. };
  592. $scope.listSizes = [5, 10, 20];
  593. PolicyProvider.query({
  594. realm : $route.current.params.realm,
  595. client : client.id
  596. }, function (data) {
  597. for (i = 0; i < data.length; i++) {
  598. if (data[i].type != 'resource' && data[i].type != 'scope') {
  599. $scope.policyProviders.push(data[i]);
  600. }
  601. }
  602. });
  603. ResourceServer.get({
  604. realm : $route.current.params.realm,
  605. client : client.id
  606. }, function(data) {
  607. $scope.server = data;
  608. $scope.searchQuery();
  609. });
  610. $scope.addPolicy = function(policyType) {
  611. if (KcStrings.endsWith(policyType.type, '.js')) {
  612. ResourceServerPolicy.save({realm : realm.realm, client : client.id, type: policyType.type}, {name: policyType.name, type: policyType.type}, function(data) {
  613. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/");
  614. Notifications.success("The policy has been created.");
  615. });
  616. } else {
  617. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/" + policyType.type + "/create");
  618. }
  619. }
  620. $scope.firstPage = function() {
  621. $scope.query.first = 0;
  622. $scope.searchQuery();
  623. }
  624. $scope.previousPage = function() {
  625. $scope.query.first -= parseInt($scope.query.max);
  626. if ($scope.query.first < 0) {
  627. $scope.query.first = 0;
  628. }
  629. $scope.searchQuery();
  630. }
  631. $scope.nextPage = function() {
  632. $scope.query.first += parseInt($scope.query.max);
  633. $scope.searchQuery();
  634. }
  635. $scope.searchQuery = function() {
  636. $scope.searchLoaded = false;
  637. ResourceServerPolicy.query($scope.query, function(data) {
  638. $scope.policies = data;
  639. $scope.searchLoaded = true;
  640. $scope.lastSearch = $scope.query.search;
  641. if ($scope.detailsFilter) {
  642. $scope.showDetails();
  643. }
  644. });
  645. };
  646. $scope.loadDetails = function (policy) {
  647. if (policy.details) {
  648. policy.details.loaded = !policy.details.loaded;
  649. return;
  650. }
  651. policy.details = {loaded: false};
  652. ResourceServerPolicy.dependentPolicies({
  653. realm : $route.current.params.realm,
  654. client : client.id,
  655. id : policy.id
  656. }, function(response) {
  657. policy.dependentPolicies = response;
  658. policy.details.loaded = true;
  659. });
  660. }
  661. $scope.showDetails = function(item, event) {
  662. if (event.target.localName == 'a' || event.target.localName == 'button') {
  663. return;
  664. }
  665. if (item) {
  666. $scope.loadDetails(item);
  667. } else {
  668. for (i = 0; i < $scope.policies.length; i++) {
  669. $scope.loadDetails($scope.policies[i]);
  670. }
  671. }
  672. };
  673. $scope.delete = function(policy) {
  674. $scope.policy = policy;
  675. Policies.delete(ResourceServerPolicy, $route.current.params.realm, client, $scope, AuthzDialog, $location, Notifications, $route, false);
  676. };
  677. });
  678. module.controller('ResourceServerPermissionCtrl', function($scope, $http, $route, $location, realm, ResourceServer, ResourceServerPermission, PolicyProvider, client, AuthzDialog, Notifications) {
  679. $scope.realm = realm;
  680. $scope.client = client;
  681. $scope.policyProviders = [];
  682. $scope.query = {
  683. realm: realm.realm,
  684. client : client.id,
  685. max : 20,
  686. first : 0
  687. };
  688. $scope.listSizes = [5, 10, 20];
  689. PolicyProvider.query({
  690. realm : $route.current.params.realm,
  691. client : client.id
  692. }, function (data) {
  693. for (i = 0; i < data.length; i++) {
  694. if (data[i].type == 'resource' || data[i].type == 'scope') {
  695. $scope.policyProviders.push(data[i]);
  696. }
  697. }
  698. });
  699. ResourceServer.get({
  700. realm : $route.current.params.realm,
  701. client : client.id
  702. }, function(data) {
  703. $scope.server = data;
  704. $scope.searchQuery();
  705. });
  706. $scope.addPolicy = function(policyType) {
  707. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/permission/" + policyType.type + "/create");
  708. }
  709. $scope.firstPage = function() {
  710. $scope.query.first = 0;
  711. $scope.searchQuery();
  712. }
  713. $scope.previousPage = function() {
  714. $scope.query.first -= parseInt($scope.query.max);
  715. if ($scope.query.first < 0) {
  716. $scope.query.first = 0;
  717. }
  718. $scope.searchQuery();
  719. }
  720. $scope.nextPage = function() {
  721. $scope.query.first += parseInt($scope.query.max);
  722. $scope.searchQuery();
  723. }
  724. $scope.searchQuery = function() {
  725. $scope.searchLoaded = false;
  726. ResourceServerPermission.query($scope.query, function(data) {
  727. $scope.policies = data;
  728. $scope.searchLoaded = true;
  729. $scope.lastSearch = $scope.query.search;
  730. if ($scope.detailsFilter) {
  731. $scope.showDetails();
  732. }
  733. });
  734. };
  735. $scope.loadDetails = function (policy) {
  736. if (policy.details) {
  737. policy.details.loaded = !policy.details.loaded;
  738. return;
  739. }
  740. policy.details = {loaded: false};
  741. ResourceServerPermission.associatedPolicies({
  742. realm : $route.current.params.realm,
  743. client : client.id,
  744. id : policy.id
  745. }, function(response) {
  746. policy.associatedPolicies = response;
  747. policy.details.loaded = true;
  748. });
  749. }
  750. $scope.showDetails = function(item, event) {
  751. if (event.target.localName == 'a' || event.target.localName == 'button') {
  752. return;
  753. }
  754. if (item) {
  755. $scope.loadDetails(item);
  756. } else {
  757. for (i = 0; i < $scope.policies.length; i++) {
  758. $scope.loadDetails($scope.policies[i]);
  759. }
  760. }
  761. };
  762. $scope.delete = function(policy) {
  763. $scope.policy = policy;
  764. Policies.delete(ResourceServerPermission, $route.current.params.realm, client, $scope, AuthzDialog, $location, Notifications, $route, true);
  765. };
  766. });
  767. module.controller('ResourceServerPolicyResourceDetailCtrl', function($scope, $route, $location, realm, client, PolicyController, ResourceServerPermission, ResourceServerResource, policyViewState) {
  768. PolicyController.onInit({
  769. getPolicyType : function() {
  770. return "resource";
  771. },
  772. isPermission : function() {
  773. return true;
  774. },
  775. onInit : function() {
  776. $scope.resourcesUiSelect = {
  777. minimumInputLength: 1,
  778. delay: 500,
  779. allowClear: true,
  780. id: function(resource){ return resource._id; },
  781. query: function (query) {
  782. var data = {results: []};
  783. if ('' == query.term.trim()) {
  784. query.callback(data);
  785. return;
  786. }
  787. $scope.query = {
  788. realm: realm.realm,
  789. client : client.id,
  790. name: query.term.trim(),
  791. deep: false,
  792. max : 20,
  793. first : 0
  794. };
  795. ResourceServerResource.query($scope.query, function(response) {
  796. data.results = response;
  797. query.callback(data);
  798. });
  799. },
  800. formatResult: function(object, container, query) {
  801. object.text = object.name;
  802. return object.name;
  803. }
  804. };
  805. $scope.policiesUiSelect = {
  806. minimumInputLength: 1,
  807. delay: 500,
  808. allowClear: true,
  809. query: function (query) {
  810. var data = {results: []};
  811. if ('' == query.term.trim()) {
  812. query.callback(data);
  813. return;
  814. }
  815. $scope.query = {
  816. realm: realm.realm,
  817. client : client.id,
  818. permission: false,
  819. name: query.term.trim(),
  820. max : 20,
  821. first : 0
  822. };
  823. ResourceServerPermission.searchPolicies($scope.query, function(response) {
  824. data.results = response;
  825. query.callback(data);
  826. });
  827. },
  828. formatResult: function(object, container, query) {
  829. object.text = object.name;
  830. return object.name;
  831. }
  832. };
  833. $scope.applyToResourceType = function() {
  834. if ($scope.applyToResourceTypeFlag) {
  835. $scope.selectedResource = null;
  836. } else {
  837. $scope.policy.resourceType = null;
  838. }
  839. }
  840. },
  841. onInitUpdate : function(policy) {
  842. if (!policy.resourceType) {
  843. $scope.selectedResource = {};
  844. ResourceServerPermission.resources({
  845. realm: $route.current.params.realm,
  846. client: client.id,
  847. id: policy.id
  848. }, function (resources) {
  849. resources[0].text = resources[0].name;
  850. $scope.selectedResource = resources[0];
  851. var copy = angular.copy($scope.selectedResource);
  852. $scope.$watch('selectedResource', function() {
  853. if (!angular.equals($scope.selectedResource, copy)) {
  854. $scope.changed = true;
  855. }
  856. }, true);
  857. });
  858. } else {
  859. $scope.applyToResourceTypeFlag = true;
  860. }
  861. ResourceServerPermission.associatedPolicies({
  862. realm : $route.current.params.realm,
  863. client : client.id,
  864. id : policy.id
  865. }, function(policies) {
  866. $scope.selectedPolicies = [];
  867. for (i = 0; i < policies.length; i++) {
  868. policies[i].text = policies[i].name;
  869. $scope.selectedPolicies.push(policies[i]);
  870. }
  871. var copy = angular.copy($scope.selectedPolicies);
  872. $scope.$watch('selectedPolicies', function() {
  873. if (!angular.equals($scope.selectedPolicies, copy)) {
  874. $scope.changed = true;
  875. }
  876. }, true);
  877. });
  878. },
  879. onUpdate : function() {
  880. if ($scope.selectedResource && $scope.selectedResource._id) {
  881. $scope.policy.resources = [];
  882. $scope.policy.resources.push($scope.selectedResource._id);
  883. } else {
  884. $scope.policy.resources = [];
  885. }
  886. var policies = [];
  887. for (i = 0; i < $scope.selectedPolicies.length; i++) {
  888. policies.push($scope.selectedPolicies[i].id);
  889. }
  890. $scope.policy.policies = policies;
  891. delete $scope.policy.config;
  892. },
  893. onInitCreate : function(newPolicy) {
  894. policyViewState.state.previousPage.name = 'authz-add-resource-permission';
  895. $scope.selectedResource = null;
  896. var copy = angular.copy($scope.selectedResource);
  897. $scope.$watch('selectedResource', function() {
  898. if (!angular.equals($scope.selectedResource, copy)) {
  899. $scope.changed = true;
  900. }
  901. }, true);
  902. $scope.selectedPolicies = null;
  903. var copy = angular.copy($scope.selectedPolicies);
  904. $scope.$watch('selectedPolicies', function() {
  905. if (!angular.equals($scope.selectedPolicies, copy)) {
  906. $scope.changed = true;
  907. }
  908. }, true);
  909. var resourceId = $location.search()['rsrid'];
  910. if (resourceId) {
  911. ResourceServerResource.get({
  912. realm : $route.current.params.realm,
  913. client : client.id,
  914. rsrid : resourceId
  915. }, function(data) {
  916. data.text = data.name;
  917. $scope.selectedResource = data;
  918. });
  919. }
  920. },
  921. onCreate : function() {
  922. if ($scope.selectedResource && $scope.selectedResource._id) {
  923. $scope.policy.resources = [];
  924. $scope.policy.resources.push($scope.selectedResource._id);
  925. } else {
  926. delete $scope.policy.resources
  927. }
  928. var policies = [];
  929. if ($scope.selectedPolicies) {
  930. for (i = 0; i < $scope.selectedPolicies.length; i++) {
  931. policies.push($scope.selectedPolicies[i].id);
  932. }
  933. }
  934. $scope.policy.policies = policies;
  935. delete $scope.policy.config;
  936. },
  937. onSaveState : function(policy) {
  938. policyViewState.state.selectedResource = $scope.selectedResource;
  939. policyViewState.state.applyToResourceTypeFlag = $scope.applyToResourceTypeFlag;
  940. },
  941. onRestoreState : function(policy) {
  942. $scope.selectedResource = policyViewState.state.selectedResource;
  943. $scope.applyToResourceTypeFlag = policyViewState.state.applyToResourceTypeFlag;
  944. policy.resourceType = policyViewState.state.policy.resourceType;
  945. }
  946. }, realm, client, $scope);
  947. });
  948. module.controller('ResourceServerPolicyScopeDetailCtrl', function($scope, $route, $location, realm, client, PolicyController, ResourceServerPolicy, ResourceServerResource, ResourceServerScope, policyViewState) {
  949. PolicyController.onInit({
  950. getPolicyType : function() {
  951. return "scope";
  952. },
  953. isPermission : function() {
  954. return true;
  955. },
  956. onInit : function() {
  957. $scope.scopesUiSelect = {
  958. minimumInputLength: 1,
  959. delay: 500,
  960. allowClear: true,
  961. query: function (query) {
  962. var data = {results: []};
  963. if ('' == query.term.trim()) {
  964. query.callback(data);
  965. return;
  966. }
  967. $scope.query = {
  968. realm: realm.realm,
  969. client : client.id,
  970. name: query.term.trim(),
  971. deep: false,
  972. max : 20,
  973. first : 0
  974. };
  975. ResourceServerScope.query($scope.query, function(response) {
  976. data.results = response;
  977. query.callback(data);
  978. });
  979. },
  980. formatResult: function(object, container, query) {
  981. object.text = object.name;
  982. return object.name;
  983. }
  984. };
  985. $scope.resourcesUiSelect = {
  986. minimumInputLength: 1,
  987. delay: 500,
  988. allowClear: true,
  989. id: function(resource){ return resource._id; },
  990. query: function (query) {
  991. var data = {results: []};
  992. if ('' == query.term.trim()) {
  993. query.callback(data);
  994. return;
  995. }
  996. $scope.query = {
  997. realm: realm.realm,
  998. client : client.id,
  999. name: query.term.trim(),
  1000. deep: false,
  1001. max : 20,
  1002. first : 0
  1003. };
  1004. ResourceServerResource.query($scope.query, function(response) {
  1005. data.results = response;
  1006. query.callback(data);
  1007. });
  1008. },
  1009. formatResult: function(object, container, query) {
  1010. object.text = object.name;
  1011. return object.name;
  1012. }
  1013. };
  1014. $scope.policiesUiSelect = {
  1015. minimumInputLength: 1,
  1016. delay: 500,
  1017. allowClear: true,
  1018. query: function (query) {
  1019. var data = {results: []};
  1020. if ('' == query.term.trim()) {
  1021. query.callback(data);
  1022. return;
  1023. }
  1024. $scope.query = {
  1025. realm: realm.realm,
  1026. client : client.id,
  1027. permission: false,
  1028. name: query.term.trim(),
  1029. max : 20,
  1030. first : 0
  1031. };
  1032. ResourceServerPolicy.query($scope.query, function(response) {
  1033. data.results = response;
  1034. query.callback(data);
  1035. });
  1036. },
  1037. formatResult: function(object, container, query) {
  1038. object.text = object.name;
  1039. return object.name;
  1040. }
  1041. };
  1042. $scope.selectResource = function() {
  1043. $scope.selectedScopes = null;
  1044. if ($scope.selectedResource) {
  1045. ResourceServerResource.scopes({
  1046. realm: $route.current.params.realm,
  1047. client: client.id,
  1048. rsrid: $scope.selectedResource._id
  1049. }, function (data) {
  1050. $scope.resourceScopes = data;
  1051. });
  1052. }
  1053. }
  1054. },
  1055. onInitUpdate : function(policy) {
  1056. ResourceServerPolicy.resources({
  1057. realm : $route.current.params.realm,
  1058. client : client.id,
  1059. id : policy.id
  1060. }, function(resources) {
  1061. if (resources.length > 0) {
  1062. for (i = 0; i < resources.length; i++) {
  1063. ResourceServerResource.get({
  1064. realm: $route.current.params.realm,
  1065. client: client.id,
  1066. rsrid: resources[0]._id,
  1067. }, function (resource) {
  1068. ResourceServerResource.query({
  1069. realm: $route.current.params.realm,
  1070. client: client.id,
  1071. _id: resource._id,
  1072. deep: false
  1073. }, function (resource) {
  1074. resource[0].text = resource[0].name;
  1075. $scope.selectedResource = resource[0];
  1076. var copy = angular.copy($scope.selectedResource);
  1077. $scope.$watch('selectedResource', function() {
  1078. if (!angular.equals($scope.selectedResource, copy)) {
  1079. $scope.changed = true;
  1080. }
  1081. }, true);
  1082. ResourceServerResource.scopes({
  1083. realm: $route.current.params.realm,
  1084. client: client.id,
  1085. rsrid: resource[0]._id
  1086. }, function (scopes) {
  1087. $scope.resourceScopes = scopes;
  1088. });
  1089. });
  1090. });
  1091. }
  1092. ResourceServerPolicy.scopes({
  1093. realm : $route.current.params.realm,
  1094. client : client.id,
  1095. id : policy.id
  1096. }, function(scopes) {
  1097. $scope.selectedScopes = [];
  1098. for (i = 0; i < scopes.length; i++) {
  1099. scopes[i].text = scopes[i].name;
  1100. $scope.selectedScopes.push(scopes[i].id);
  1101. }
  1102. var copy = angular.copy($scope.selectedScopes);
  1103. $scope.$watch('selectedScopes', function() {
  1104. if (!angular.equals($scope.selectedScopes, copy)) {
  1105. $scope.changed = true;
  1106. }
  1107. }, true);
  1108. });
  1109. } else {
  1110. $scope.selectedResource = null;
  1111. var copy = angular.copy($scope.selectedResource);
  1112. $scope.$watch('selectedResource', function() {
  1113. if (!angular.equals($scope.selectedResource, copy)) {
  1114. $scope.changed = true;
  1115. }
  1116. }, true);
  1117. ResourceServerPolicy.scopes({
  1118. realm : $route.current.params.realm,
  1119. client : client.id,
  1120. id : policy.id
  1121. }, function(scopes) {
  1122. $scope.selectedScopes = [];
  1123. for (i = 0; i < scopes.length; i++) {
  1124. scopes[i].text = scopes[i].name;
  1125. $scope.selectedScopes.push(scopes[i]);
  1126. }
  1127. var copy = angular.copy($scope.selectedScopes);
  1128. $scope.$watch('selectedScopes', function() {
  1129. if (!angular.equals($scope.selectedScopes, copy)) {
  1130. $scope.changed = true;
  1131. }
  1132. }, true);
  1133. });
  1134. }
  1135. });
  1136. ResourceServerPolicy.associatedPolicies({
  1137. realm : $route.current.params.realm,
  1138. client : client.id,
  1139. id : policy.id
  1140. }, function(policies) {
  1141. $scope.selectedPolicies = [];
  1142. for (i = 0; i < policies.length; i++) {
  1143. policies[i].text = policies[i].name;
  1144. $scope.selectedPolicies.push(policies[i]);
  1145. }
  1146. var copy = angular.copy($scope.selectedPolicies);
  1147. $scope.$watch('selectedPolicies', function() {
  1148. if (!angular.equals($scope.selectedPolicies, copy)) {
  1149. $scope.changed = true;
  1150. }
  1151. }, true);
  1152. });
  1153. },
  1154. onUpdate : function() {
  1155. if ($scope.selectedResource != null) {
  1156. $scope.policy.resources = [$scope.selectedResource._id];
  1157. } else {
  1158. $scope.policy.resources = [];
  1159. }
  1160. var scopes = [];
  1161. for (i = 0; i < $scope.selectedScopes.length; i++) {
  1162. if ($scope.selectedScopes[i].id) {
  1163. scopes.push($scope.selectedScopes[i].id);
  1164. } else {
  1165. scopes.push($scope.selectedScopes[i]);
  1166. }
  1167. }
  1168. $scope.policy.scopes = scopes;
  1169. var policies = [];
  1170. if ($scope.selectedPolicies) {
  1171. for (i = 0; i < $scope.selectedPolicies.length; i++) {
  1172. policies.push($scope.selectedPolicies[i].id);
  1173. }
  1174. }
  1175. $scope.policy.policies = policies;
  1176. delete $scope.policy.config;
  1177. },
  1178. onInitCreate : function(newPolicy) {
  1179. policyViewState.state.previousPage.name = 'authz-add-scope-permission';
  1180. var scopeId = $location.search()['scpid'];
  1181. if (scopeId) {
  1182. ResourceServerScope.get({
  1183. realm: $route.current.params.realm,
  1184. client: client.id,
  1185. id: scopeId,
  1186. }, function (data) {
  1187. data.text = data.name;
  1188. if (!$scope.policy.scopes) {
  1189. $scope.selectedScopes = [];
  1190. }
  1191. $scope.selectedScopes.push(data);
  1192. });
  1193. }
  1194. },
  1195. onCreate : function() {
  1196. if ($scope.selectedResource != null) {
  1197. $scope.policy.resources = [$scope.selectedResource._id];
  1198. }
  1199. var scopes = [];
  1200. for (i = 0; i < $scope.selectedScopes.length; i++) {
  1201. if ($scope.selectedScopes[i].id) {
  1202. scopes.push($scope.selectedScopes[i].id);
  1203. } else {
  1204. scopes.push($scope.selectedScopes[i]);
  1205. }
  1206. }
  1207. $scope.policy.scopes = scopes;
  1208. var policies = [];
  1209. if ($scope.selectedPolicies) {
  1210. for (i = 0; i < $scope.selectedPolicies.length; i++) {
  1211. policies.push($scope.selectedPolicies[i].id);
  1212. }
  1213. }
  1214. $scope.policy.policies = policies;
  1215. delete $scope.policy.config;
  1216. },
  1217. onSaveState : function(policy) {
  1218. policyViewState.state.selectedScopes = $scope.selectedScopes;
  1219. policyViewState.state.selectedResource = $scope.selectedResource;
  1220. policyViewState.state.resourceScopes = $scope.resourceScopes;
  1221. },
  1222. onRestoreState : function(policy) {
  1223. $scope.selectedScopes = policyViewState.state.selectedScopes;
  1224. $scope.selectedResource = policyViewState.state.selectedResource;
  1225. $scope.resourceScopes = policyViewState.state.resourceScopes;
  1226. }
  1227. }, realm, client, $scope);
  1228. });
  1229. module.controller('ResourceServerPolicyUserDetailCtrl', function($scope, $route, realm, client, PolicyController, User) {
  1230. PolicyController.onInit({
  1231. getPolicyType : function() {
  1232. return "user";
  1233. },
  1234. onInit : function() {
  1235. $scope.usersUiSelect = {
  1236. minimumInputLength: 1,
  1237. delay: 500,
  1238. allowClear: true,
  1239. query: function (query) {
  1240. var data = {results: []};
  1241. if ('' == query.term.trim()) {
  1242. query.callback(data);
  1243. return;
  1244. }
  1245. User.query({realm: $route.current.params.realm, search: query.term.trim(), max: 20}, function(response) {
  1246. data.results = response;
  1247. query.callback(data);
  1248. });
  1249. },
  1250. formatResult: function(object, container, query) {
  1251. return object.username;
  1252. }
  1253. };
  1254. $scope.selectedUsers = [];
  1255. $scope.selectUser = function(user) {
  1256. if (!user || !user.id) {
  1257. return;
  1258. }
  1259. $scope.selectedUser = null;
  1260. for (i = 0; i < $scope.selectedUsers.length; i++) {
  1261. if ($scope.selectedUsers[i].id == user.id) {
  1262. return;
  1263. }
  1264. }
  1265. $scope.selectedUsers.push(user);
  1266. }
  1267. $scope.removeFromList = function(list, user) {
  1268. for (i = 0; i < angular.copy(list).length; i++) {
  1269. if (user == list[i]) {
  1270. list.splice(i, 1);
  1271. }
  1272. }
  1273. }
  1274. },
  1275. onInitUpdate : function(policy) {
  1276. var selectedUsers = [];
  1277. if (policy.users) {
  1278. var users = policy.users;
  1279. for (i = 0; i < users.length; i++) {
  1280. User.get({realm: $route.current.params.realm, userId: users[i]}, function(data) {
  1281. selectedUsers.push(data);
  1282. $scope.selectedUsers = angular.copy(selectedUsers);
  1283. });
  1284. }
  1285. }
  1286. $scope.$watch('selectedUsers', function() {
  1287. if (!angular.equals($scope.selectedUsers, selectedUsers)) {
  1288. $scope.changed = true;
  1289. } else {
  1290. $scope.changed = false;
  1291. }
  1292. }, true);
  1293. },
  1294. onUpdate : function() {
  1295. var users = [];
  1296. for (i = 0; i < $scope.selectedUsers.length; i++) {
  1297. users.push($scope.selectedUsers[i].id);
  1298. }
  1299. $scope.policy.users = users;
  1300. delete $scope.policy.config;
  1301. },
  1302. onCreate : function() {
  1303. var users = [];
  1304. for (i = 0; i < $scope.selectedUsers.length; i++) {
  1305. users.push($scope.selectedUsers[i].id);
  1306. }
  1307. $scope.policy.users = users;
  1308. delete $scope.policy.config;
  1309. }
  1310. }, realm, client, $scope);
  1311. });
  1312. module.controller('ResourceServerPolicyClientDetailCtrl', function($scope, $route, realm, client, PolicyController, Client) {
  1313. PolicyController.onInit({
  1314. getPolicyType : function() {
  1315. return "client";
  1316. },
  1317. onInit : function() {
  1318. clientSelectControl($scope, $route.current.params.realm, Client);
  1319. $scope.selectedClients = [];
  1320. $scope.selectClient = function(client) {
  1321. if (!client || !client.id) {
  1322. return;
  1323. }
  1324. $scope.selectedClient = null;
  1325. for (var i = 0; i < $scope.selectedClients.length; i++) {
  1326. if ($scope.selectedClients[i].id == client.id) {
  1327. return;
  1328. }
  1329. }
  1330. $scope.selectedClients.push(client);
  1331. }
  1332. $scope.removeFromList = function(client) {
  1333. var index = $scope.selectedClients.indexOf(client);
  1334. if (index != -1) {
  1335. $scope.selectedClients.splice(index, 1);
  1336. }
  1337. }
  1338. },
  1339. onInitUpdate : function(policy) {
  1340. var selectedClients = [];
  1341. if (policy.clients) {
  1342. var clients = policy.clients;
  1343. for (var i = 0; i < clients.length; i++) {
  1344. Client.get({realm: $route.current.params.realm, client: clients[i]}, function(data) {
  1345. selectedClients.push(data);
  1346. $scope.selectedClients = angular.copy(selectedClients);
  1347. });
  1348. }
  1349. }
  1350. $scope.$watch('selectedClients', function() {
  1351. if (!angular.equals($scope.selectedClients, selectedClients)) {
  1352. $scope.changed = true;
  1353. } else {
  1354. $scope.changed = false;
  1355. }
  1356. }, true);
  1357. },
  1358. onUpdate : function() {
  1359. var clients = [];
  1360. for (var i = 0; i < $scope.selectedClients.length; i++) {
  1361. clients.push($scope.selectedClients[i].id);
  1362. }
  1363. $scope.policy.clients = clients;
  1364. delete $scope.policy.config;
  1365. },
  1366. onInitCreate : function() {
  1367. var selectedClients = [];
  1368. $scope.$watch('selectedClients', function() {
  1369. if (!angular.equals($scope.selectedClients, selectedClients)) {
  1370. $scope.changed = true;
  1371. }
  1372. }, true);
  1373. },
  1374. onCreate : function() {
  1375. var clients = [];
  1376. for (var i = 0; i < $scope.selectedClients.length; i++) {
  1377. clients.push($scope.selectedClients[i].id);
  1378. }
  1379. $scope.policy.clients = clients;
  1380. delete $scope.policy.config;
  1381. }
  1382. }, realm, client, $scope);
  1383. });
  1384. module.controller('ResourceServerPolicyRoleDetailCtrl', function($scope, $route, realm, client, Client, ClientRole, PolicyController, Role, RoleById) {
  1385. PolicyController.onInit({
  1386. getPolicyType : function() {
  1387. return "role";
  1388. },
  1389. onInit : function() {
  1390. Role.query({realm: $route.current.params.realm}, function(data) {
  1391. $scope.roles = data;
  1392. });
  1393. Client.query({realm: $route.current.params.realm}, function (data) {
  1394. $scope.clients = data;
  1395. });
  1396. $scope.selectedRoles = [];
  1397. $scope.selectRole = function(role) {
  1398. if (!role || !role.id) {
  1399. return;
  1400. }
  1401. $scope.selectedRole = null;
  1402. for (i = 0; i < $scope.selectedRoles.length; i++) {
  1403. if ($scope.selectedRoles[i].id == role.id) {
  1404. return;
  1405. }
  1406. }
  1407. $scope.selectedRoles.push(role);
  1408. var clientRoles = [];
  1409. if ($scope.clientRoles) {
  1410. for (i = 0; i < $scope.clientRoles.length; i++) {
  1411. if ($scope.clientRoles[i].id != role.id) {
  1412. clientRoles.push($scope.clientRoles[i]);
  1413. }
  1414. }
  1415. $scope.clientRoles = clientRoles;
  1416. }
  1417. }
  1418. $scope.removeFromList = function(role) {
  1419. if ($scope.clientRoles && $scope.selectedClient && $scope.selectedClient.id == role.containerId) {
  1420. $scope.clientRoles.push(role);
  1421. }
  1422. var index = $scope.selectedRoles.indexOf(role);
  1423. if (index != -1) {
  1424. $scope.selectedRoles.splice(index, 1);
  1425. }
  1426. }
  1427. $scope.selectClient = function() {
  1428. if (!$scope.selectedClient) {
  1429. $scope.clientRoles = [];
  1430. return;
  1431. }
  1432. ClientRole.query({realm: $route.current.params.realm, client: $scope.selectedClient.id}, function(data) {
  1433. var roles = [];
  1434. for (j = 0; j < data.length; j++) {
  1435. var defined = false;
  1436. for (i = 0; i < $scope.selectedRoles.length; i++) {
  1437. if ($scope.selectedRoles[i].id == data[j].id) {
  1438. defined = true;
  1439. break;
  1440. }
  1441. }
  1442. if (!defined) {
  1443. data[j].container = {};
  1444. data[j].container.name = $scope.selectedClient.clientId;
  1445. roles.push(data[j]);
  1446. }
  1447. }
  1448. $scope.clientRoles = roles;
  1449. });
  1450. }
  1451. },
  1452. onInitUpdate : function(policy) {
  1453. var selectedRoles = [];
  1454. if (policy.roles) {
  1455. var roles = policy.roles;
  1456. for (i = 0; i < roles.length; i++) {
  1457. RoleById.get({realm: $route.current.params.realm, role: roles[i].id}, function(data) {
  1458. for (i = 0; i < roles.length; i++) {
  1459. if (roles[i].id == data.id) {
  1460. data.required = roles[i].required ? true : false;
  1461. }
  1462. }
  1463. for (i = 0; i < $scope.clients.length; i++) {
  1464. if ($scope.clients[i].id == data.containerId) {
  1465. data.container = {};
  1466. data.container.name = $scope.clients[i].clientId;
  1467. }
  1468. }
  1469. selectedRoles.push(data);
  1470. $scope.selectedRoles = angular.copy(selectedRoles);
  1471. });
  1472. }
  1473. }
  1474. $scope.$watch('selectedRoles', function() {
  1475. if (!angular.equals($scope.selectedRoles, selectedRoles)) {
  1476. $scope.changed = true;
  1477. } else {
  1478. $scope.changed = false;
  1479. }
  1480. }, true);
  1481. },
  1482. onUpdate : function() {
  1483. var roles = [];
  1484. for (i = 0; i < $scope.selectedRoles.length; i++) {
  1485. var role = {};
  1486. role.id = $scope.selectedRoles[i].id;
  1487. if ($scope.selectedRoles[i].required) {
  1488. role.required = $scope.selectedRoles[i].required;
  1489. }
  1490. roles.push(role);
  1491. }
  1492. $scope.policy.roles = roles;
  1493. delete $scope.policy.config;
  1494. },
  1495. onCreate : function() {
  1496. var roles = [];
  1497. for (i = 0; i < $scope.selectedRoles.length; i++) {
  1498. var role = {};
  1499. role.id = $scope.selectedRoles[i].id;
  1500. if ($scope.selectedRoles[i].required) {
  1501. role.required = $scope.selectedRoles[i].required;
  1502. }
  1503. roles.push(role);
  1504. }
  1505. $scope.policy.roles = roles;
  1506. delete $scope.policy.config;
  1507. }
  1508. }, realm, client, $scope);
  1509. $scope.hasRealmRole = function () {
  1510. for (i = 0; i < $scope.selectedRoles.length; i++) {
  1511. if (!$scope.selectedRoles[i].clientRole) {
  1512. return true;
  1513. }
  1514. }
  1515. return false;
  1516. }
  1517. $scope.hasClientRole = function () {
  1518. for (i = 0; i < $scope.selectedRoles.length; i++) {
  1519. if ($scope.selectedRoles[i].clientRole) {
  1520. return true;
  1521. }
  1522. }
  1523. return false;
  1524. }
  1525. });
  1526. module.controller('ResourceServerPolicyGroupDetailCtrl', function($scope, $route, realm, client, Client, Groups, Group, PolicyController, Notifications, $translate) {
  1527. PolicyController.onInit({
  1528. getPolicyType : function() {
  1529. return "group";
  1530. },
  1531. onInit : function() {
  1532. $scope.tree = [];
  1533. Groups.query({realm: $route.current.params.realm}, function(groups) {
  1534. $scope.groups = groups;
  1535. $scope.groupList = [
  1536. {"id" : "realm", "name": $translate.instant('groups'),
  1537. "subGroups" : groups}
  1538. ];
  1539. });
  1540. var isLeaf = function(node) {
  1541. return node.id != "realm" && (!node.subGroups || node.subGroups.length == 0);
  1542. }
  1543. $scope.getGroupClass = function(node) {
  1544. if (node.id == "realm") {
  1545. return 'pficon pficon-users';
  1546. }
  1547. if (isLeaf(node)) {
  1548. return 'normal';
  1549. }
  1550. if (node.subGroups.length && node.collapsed) return 'collapsed';
  1551. if (node.subGroups.length && !node.collapsed) return 'expanded';
  1552. return 'collapsed';
  1553. }
  1554. $scope.getSelectedClass = function(node) {
  1555. if (node.selected) {
  1556. return 'selected';
  1557. } else if ($scope.cutNode && $scope.cutNode.id == node.id) {
  1558. return 'cut';
  1559. }
  1560. return undefined;
  1561. }
  1562. $scope.selectGroup = function(group) {
  1563. for (i = 0; i < $scope.selectedGroups.length; i++) {
  1564. if ($scope.selectedGroups[i].id == group.id) {
  1565. return
  1566. }
  1567. }
  1568. if (group.id == "realm") {
  1569. Notifications.error("You must choose a group");
  1570. return;
  1571. }
  1572. $scope.selectedGroups.push({id: group.id, path: group.path});
  1573. $scope.changed = true;
  1574. }
  1575. $scope.extendChildren = function(group) {
  1576. $scope.changed = true;
  1577. }
  1578. $scope.removeFromList = function(group) {
  1579. var index = $scope.selectedGroups.indexOf(group);
  1580. if (index != -1) {
  1581. $scope.selectedGroups.splice(index, 1);
  1582. $scope.changed = true;
  1583. }
  1584. }
  1585. },
  1586. onInitCreate : function(policy) {
  1587. var selectedGroups = [];
  1588. $scope.selectedGroups = angular.copy(selectedGroups);
  1589. $scope.$watch('selectedGroups', function() {
  1590. if (!angular.equals($scope.selectedGroups, selectedGroups)) {
  1591. $scope.changed = true;
  1592. } else {
  1593. $scope.changed = PolicyController.isNewAssociatedPolicy();
  1594. }
  1595. }, true);
  1596. },
  1597. onInitUpdate : function(policy) {
  1598. $scope.selectedGroups = policy.groups;
  1599. angular.forEach($scope.selectedGroups, function(group, index){
  1600. Group.get({realm: $route.current.params.realm, groupId: group.id}, function (existing) {
  1601. group.path = existing.path;
  1602. });
  1603. });
  1604. $scope.$watch('selectedGroups', function() {
  1605. if (!$scope.changed) {
  1606. return;
  1607. }
  1608. if (!angular.equals($scope.selectedGroups, selectedGroups)) {
  1609. $scope.changed = true;
  1610. } else {
  1611. $scope.changed = false;
  1612. }
  1613. }, true);
  1614. },
  1615. onUpdate : function() {
  1616. $scope.policy.groups = $scope.selectedGroups;
  1617. delete $scope.policy.config;
  1618. },
  1619. onCreate : function() {
  1620. $scope.policy.groups = $scope.selectedGroups;
  1621. delete $scope.policy.config;
  1622. }
  1623. }, realm, client, $scope);
  1624. });
  1625. module.controller('ResourceServerPolicyJSDetailCtrl', function($scope, $route, $location, realm, PolicyController, client, serverInfo) {
  1626. PolicyController.onInit({
  1627. getPolicyType : function() {
  1628. return "js";
  1629. },
  1630. onInit : function() {
  1631. $scope.readOnly = !serverInfo.featureEnabled('UPLOAD_SCRIPTS');
  1632. $scope.initEditor = function(editor){
  1633. editor.$blockScrolling = Infinity;
  1634. editor.setReadOnly($scope.readOnly);
  1635. var session = editor.getSession();
  1636. session.setMode('ace/mode/javascript');
  1637. };
  1638. },
  1639. onInitUpdate : function(policy) {
  1640. },
  1641. onUpdate : function() {
  1642. delete $scope.policy.config;
  1643. },
  1644. onInitCreate : function(newPolicy) {
  1645. },
  1646. onCreate : function() {
  1647. delete $scope.policy.config;
  1648. }
  1649. }, realm, client, $scope);
  1650. });
  1651. module.controller('ResourceServerPolicyTimeDetailCtrl', function($scope, $route, $location, realm, PolicyController, client) {
  1652. function clearEmptyStrings() {
  1653. if ($scope.policy.notBefore != undefined && $scope.policy.notBefore.trim() == '') {
  1654. $scope.policy.notBefore = null;
  1655. }
  1656. if ($scope.policy.notOnOrAfter != undefined && $scope.policy.notOnOrAfter.trim() == '') {
  1657. $scope.policy.notOnOrAfter = null;
  1658. }
  1659. }
  1660. PolicyController.onInit({
  1661. getPolicyType : function() {
  1662. return "time";
  1663. },
  1664. onInit : function() {
  1665. },
  1666. onInitUpdate : function(policy) {
  1667. if (policy.dayMonth) {
  1668. policy.dayMonth = parseInt(policy.dayMonth);
  1669. }
  1670. if (policy.dayMonthEnd) {
  1671. policy.dayMonthEnd = parseInt(policy.dayMonthEnd);
  1672. }
  1673. if (policy.month) {
  1674. policy.month = parseInt(policy.month);
  1675. }
  1676. if (policy.monthEnd) {
  1677. policy.monthEnd = parseInt(policy.monthEnd);
  1678. }
  1679. if (policy.year) {
  1680. policy.year = parseInt(policy.year);
  1681. }
  1682. if (policy.yearEnd) {
  1683. policy.yearEnd = parseInt(policy.yearEnd);
  1684. }
  1685. if (policy.hour) {
  1686. policy.hour = parseInt(policy.hour);
  1687. }
  1688. if (policy.hourEnd) {
  1689. policy.hourEnd = parseInt(policy.hourEnd);
  1690. }
  1691. if (policy.minute) {
  1692. policy.minute = parseInt(policy.minute);
  1693. }
  1694. if (policy.minuteEnd) {
  1695. policy.minuteEnd = parseInt(policy.minuteEnd);
  1696. }
  1697. },
  1698. onUpdate : function() {
  1699. clearEmptyStrings();
  1700. delete $scope.policy.config;
  1701. },
  1702. onInitCreate : function(newPolicy) {
  1703. },
  1704. onCreate : function() {
  1705. clearEmptyStrings();
  1706. delete $scope.policy.config;
  1707. }
  1708. }, realm, client, $scope);
  1709. $scope.isRequired = function () {
  1710. var policy = $scope.policy;
  1711. if (!policy) {
  1712. return true;
  1713. }
  1714. if (policy.notOnOrAfter || policy.notBefore
  1715. || policy.dayMonth
  1716. || policy.month
  1717. || policy.year
  1718. || policy.hour
  1719. || policy.minute) {
  1720. return false;
  1721. }
  1722. return true;
  1723. }
  1724. });
  1725. module.controller('ResourceServerPolicyAggregateDetailCtrl', function($scope, $route, $location, realm, PolicyController, ResourceServerPolicy, client, PolicyProvider, policyViewState) {
  1726. PolicyController.onInit({
  1727. getPolicyType : function() {
  1728. return "aggregate";
  1729. },
  1730. onInit : function() {
  1731. $scope.policiesUiSelect = {
  1732. minimumInputLength: 1,
  1733. delay: 500,
  1734. allowClear: true,
  1735. query: function (query) {
  1736. var data = {results: []};
  1737. if ('' == query.term.trim()) {
  1738. query.callback(data);
  1739. return;
  1740. }
  1741. $scope.query = {
  1742. realm: realm.realm,
  1743. client : client.id,
  1744. permission: false,
  1745. name: query.term.trim(),
  1746. max : 20,
  1747. first : 0
  1748. };
  1749. ResourceServerPolicy.query($scope.query, function(response) {
  1750. data.results = response;
  1751. query.callback(data);
  1752. });
  1753. },
  1754. formatResult: function(object, container, query) {
  1755. object.text = object.name;
  1756. return object.name;
  1757. }
  1758. };
  1759. },
  1760. onInitUpdate : function(policy) {
  1761. ResourceServerPolicy.associatedPolicies({
  1762. realm : $route.current.params.realm,
  1763. client : client.id,
  1764. id : policy.id
  1765. }, function(policies) {
  1766. $scope.selectedPolicies = [];
  1767. for (i = 0; i < policies.length; i++) {
  1768. policies[i].text = policies[i].name;
  1769. $scope.selectedPolicies.push(policies[i]);
  1770. }
  1771. var copy = angular.copy($scope.selectedPolicies);
  1772. $scope.$watch('selectedPolicies', function() {
  1773. if (!angular.equals($scope.selectedPolicies, copy)) {
  1774. $scope.changed = true;
  1775. }
  1776. }, true);
  1777. });
  1778. },
  1779. onUpdate : function() {
  1780. var policies = [];
  1781. for (i = 0; i < $scope.selectedPolicies.length; i++) {
  1782. policies.push($scope.selectedPolicies[i].id);
  1783. }
  1784. $scope.policy.policies = policies;
  1785. delete $scope.policy.config;
  1786. },
  1787. onInitCreate : function(newPolicy) {
  1788. policyViewState.state.previousPage.name = 'authz-add-aggregated-policy';
  1789. },
  1790. onCreate : function() {
  1791. var policies = [];
  1792. for (i = 0; i < $scope.selectedPolicies.length; i++) {
  1793. policies.push($scope.selectedPolicies[i].id);
  1794. }
  1795. $scope.policy.policies = policies;
  1796. delete $scope.policy.config;
  1797. }
  1798. }, realm, client, $scope);
  1799. });
  1800. module.controller('ResourceServerPolicyClientScopeDetailCtrl', function($scope, $route, realm, client, ClientScope, PolicyController) {
  1801. PolicyController.onInit({
  1802. getPolicyType : function() {
  1803. return "client-scope";
  1804. },
  1805. onInit : function() {
  1806. ClientScope.query({realm: $route.current.params.realm}, function(data) {
  1807. $scope.clientScopes = data;
  1808. });
  1809. $scope.selectedClientScopes = [];
  1810. $scope.selectClientScope = function(clientScope) {
  1811. if (!clientScope || !clientScope.id) {
  1812. return;
  1813. }
  1814. $scope.selectedClientScope = null;
  1815. for (i = 0; i < $scope.selectedClientScopes.length; i++) {
  1816. if ($scope.selectedClientScopes[i].id == clientScope.id) {
  1817. return;
  1818. }
  1819. }
  1820. $scope.selectedClientScopes.push(clientScope);
  1821. }
  1822. $scope.removeFromList = function(clientScope) {
  1823. var index = $scope.selectedClientScopes.indexOf(clientScope);
  1824. if (index != -1) {
  1825. $scope.selectedClientScopes.splice(index, 1);
  1826. }
  1827. }
  1828. },
  1829. onInitUpdate : function(policy) {
  1830. var selectedClientScopes = [];
  1831. if (policy.clientScopes) {
  1832. var clientScopes = policy.clientScopes;
  1833. for (i = 0; i < clientScopes.length; i++) {
  1834. ClientScope.get({realm: $route.current.params.realm, clientScope: clientScopes[i].id}, function(data) {
  1835. for (i = 0; i < clientScopes.length; i++) {
  1836. if (clientScopes[i].id == data.id) {
  1837. data.required = clientScopes[i].required ? true : false;
  1838. }
  1839. }
  1840. selectedClientScopes.push(data);
  1841. $scope.selectedClientScopes = angular.copy(selectedClientScopes);
  1842. });
  1843. }
  1844. }
  1845. $scope.$watch('selectedClientScopes', function() {
  1846. if (!angular.equals($scope.selectedClientScopes, selectedClientScopes)) {
  1847. $scope.changed = true;
  1848. } else {
  1849. $scope.changed = false;
  1850. }
  1851. }, true);
  1852. },
  1853. onUpdate : function() {
  1854. var clientScopes = [];
  1855. for (i = 0; i < $scope.selectedClientScopes.length; i++) {
  1856. var clientScope = {};
  1857. clientScope.id = $scope.selectedClientScopes[i].id;
  1858. if ($scope.selectedClientScopes[i].required) {
  1859. clientScope.required = $scope.selectedClientScopes[i].required;
  1860. }
  1861. clientScopes.push(clientScope);
  1862. }
  1863. $scope.policy.clientScopes = clientScopes;
  1864. delete $scope.policy.config;
  1865. },
  1866. onCreate : function() {
  1867. var clientScopes = [];
  1868. for (i = 0; i < $scope.selectedClientScopes.length; i++) {
  1869. var clientScope = {};
  1870. clientScope.id = $scope.selectedClientScopes[i].id;
  1871. if ($scope.selectedClientScopes[i].required) {
  1872. clientScope.required = $scope.selectedClientScopes[i].required;
  1873. }
  1874. clientScopes.push(clientScope);
  1875. }
  1876. $scope.policy.clientScopes = clientScopes;
  1877. delete $scope.policy.config;
  1878. }
  1879. }, realm, client, $scope);
  1880. });
  1881. module.controller('ResourceServerPolicyRegexDetailCtrl', function($scope, realm, client, PolicyController) {
  1882. PolicyController.onInit({
  1883. getPolicyType : function() {
  1884. return "regex";
  1885. },
  1886. onInit : function() {
  1887. },
  1888. onInitUpdate : function(policy) {
  1889. },
  1890. onUpdate : function() {
  1891. delete $scope.policy.config;
  1892. },
  1893. onCreate : function() {
  1894. delete $scope.policy.config;
  1895. }
  1896. }, realm, client, $scope);
  1897. });
  1898. module.service("PolicyController", function($http, $route, $location, ResourceServer, ResourceServerPolicy, ResourceServerPermission, AuthzDialog, Notifications, policyViewState, PolicyProvider, viewState) {
  1899. var PolicyController = {};
  1900. PolicyController.isNewAssociatedPolicy = function() {
  1901. return $route.current.params['new_policy'] != null;
  1902. }
  1903. PolicyController.isBackNewAssociatedPolicy = function() {
  1904. return $route.current.params['back'] != null;
  1905. }
  1906. PolicyController.onInit = function(delegate, realm, client, $scope) {
  1907. $scope.policyProviders = [];
  1908. PolicyProvider.query({
  1909. realm : $route.current.params.realm,
  1910. client : client.id
  1911. }, function (data) {
  1912. for (i = 0; i < data.length; i++) {
  1913. if (data[i].type != 'resource' && data[i].type != 'scope') {
  1914. $scope.policyProviders.push(data[i]);
  1915. }
  1916. }
  1917. });
  1918. if ((!policyViewState.state || !PolicyController.isBackNewAssociatedPolicy()) && !PolicyController.isNewAssociatedPolicy()) {
  1919. policyViewState.state = {};
  1920. }
  1921. if (!policyViewState.state.previousPage) {
  1922. policyViewState.state.previousPage = {};
  1923. }
  1924. $scope.policyViewState = policyViewState;
  1925. $scope.addPolicy = function(policyType) {
  1926. policyViewState.state.policy = $scope.policy;
  1927. if (delegate.onSaveState) {
  1928. delegate.onSaveState($scope.policy);
  1929. }
  1930. if ($scope.selectedPolicies) {
  1931. policyViewState.state.selectedPolicies = $scope.selectedPolicies;
  1932. }
  1933. var previousUrl = window.location.href.substring(window.location.href.indexOf('/realms'));
  1934. if (previousUrl.indexOf('back=true') == -1) {
  1935. previousUrl = previousUrl + (previousUrl.indexOf('?') == -1 ? '?' : '&') + 'back=true';
  1936. }
  1937. policyViewState.state.previousUrl = previousUrl;
  1938. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/" + policyType.type + "/create?new_policy=true");
  1939. }
  1940. $scope.detailPolicy = function(policy) {
  1941. policyViewState.state.policy = $scope.policy;
  1942. if (delegate.onSaveState) {
  1943. delegate.onSaveState($scope.policy);
  1944. }
  1945. if ($scope.selectedPolicies) {
  1946. policyViewState.state.selectedPolicies = $scope.selectedPolicies;
  1947. }
  1948. var previousUrl = window.location.href.substring(window.location.href.indexOf('/realms'));
  1949. if (previousUrl.indexOf('back=true') == -1) {
  1950. previousUrl = previousUrl + (previousUrl.indexOf('?') == -1 ? '?' : '&') + 'back=true';
  1951. }
  1952. policyViewState.state.previousUrl = previousUrl;
  1953. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/" + policy.type + "/" + policy.id + "?new_policy=true");
  1954. }
  1955. $scope.removePolicy = function(list, policy) {
  1956. for (i = 0; i < angular.copy(list).length; i++) {
  1957. if (policy.id == list[i].id) {
  1958. list.splice(i, 1);
  1959. }
  1960. }
  1961. }
  1962. $scope.selectPolicy = function(policy) {
  1963. if (!policy || !policy.id) {
  1964. return;
  1965. }
  1966. if (!$scope.selectedPolicies) {
  1967. $scope.selectedPolicies = [];
  1968. }
  1969. $scope.selectedPolicy = null;
  1970. for (i = 0; i < $scope.selectedPolicies.length; i++) {
  1971. if ($scope.selectedPolicies[i].id == policy.id) {
  1972. return;
  1973. }
  1974. }
  1975. $scope.selectedPolicies.push(policy);
  1976. }
  1977. $scope.createNewPolicy = function() {
  1978. $scope.showNewPolicy = true;
  1979. }
  1980. $scope.cancelCreateNewPolicy = function() {
  1981. $scope.showNewPolicy = false;
  1982. }
  1983. $scope.historyBackOnSaveOrCancel = PolicyController.isNewAssociatedPolicy();
  1984. if (!delegate.isPermission) {
  1985. delegate.isPermission = function () {
  1986. return false;
  1987. }
  1988. }
  1989. var service = ResourceServerPolicy;
  1990. if (delegate.isPermission()) {
  1991. service = ResourceServerPermission;
  1992. }
  1993. $scope.realm = realm;
  1994. $scope.client = client;
  1995. $scope.decisionStrategies = ['AFFIRMATIVE', 'UNANIMOUS', 'CONSENSUS'];
  1996. $scope.logics = ['POSITIVE', 'NEGATIVE'];
  1997. delegate.onInit();
  1998. var $instance = this;
  1999. ResourceServer.get({
  2000. realm : $route.current.params.realm,
  2001. client : client.id
  2002. }, function(data) {
  2003. $scope.server = data;
  2004. var policyId = $route.current.params.id;
  2005. if (!policyId) {
  2006. $scope.create = true;
  2007. var policy = {};
  2008. policy.type = delegate.getPolicyType();
  2009. policy.config = {};
  2010. policy.logic = 'POSITIVE';
  2011. policy.decisionStrategy = 'UNANIMOUS';
  2012. $scope.changed = $scope.historyBackOnSaveOrCancel || PolicyController.isBackNewAssociatedPolicy();
  2013. if (viewState.state != null && viewState.state.previousUrl != null) {
  2014. $scope.previousUrl = viewState.state.previousUrl;
  2015. policyViewState.state.rootUrl = $scope.previousUrl;
  2016. viewState.state = {};
  2017. }
  2018. $scope.policy = angular.copy(policy);
  2019. $scope.$watch('policy', function() {
  2020. if (!angular.equals($scope.policy, policy)) {
  2021. $scope.changed = true;
  2022. }
  2023. }, true);
  2024. if (PolicyController.isBackNewAssociatedPolicy()) {
  2025. if (delegate.onRestoreState) {
  2026. delegate.onRestoreState($scope.policy);
  2027. }
  2028. $instance.restoreState($scope);
  2029. } else if (delegate.onInitCreate) {
  2030. delegate.onInitCreate(policy);
  2031. }
  2032. $scope.save = function() {
  2033. $instance.checkNameAvailability(function () {
  2034. if (delegate.onCreate) {
  2035. delegate.onCreate();
  2036. }
  2037. service.save({realm : realm.realm, client : client.id, type: $scope.policy.type}, $scope.policy, function(data) {
  2038. if (delegate.isPermission()) {
  2039. if ($scope.historyBackOnSaveOrCancel || policyViewState.state.rootUrl != null) {
  2040. if (policyViewState.state.rootUrl != null) {
  2041. $location.url(policyViewState.state.rootUrl);
  2042. } else {
  2043. policyViewState.state.newPolicyName = $scope.policy.name;
  2044. $location.url(policyViewState.state.previousUrl);
  2045. }
  2046. } else {
  2047. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/permission/" + $scope.policy.type + "/" + data.id);
  2048. }
  2049. Notifications.success("The permission has been created.");
  2050. } else {
  2051. if ($scope.historyBackOnSaveOrCancel) {
  2052. policyViewState.state.newPolicyName = $scope.policy.name;
  2053. $location.url(policyViewState.state.previousUrl);
  2054. } else {
  2055. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/" + $scope.policy.type + "/" + data.id);
  2056. }
  2057. Notifications.success("The policy has been created.");
  2058. }
  2059. });
  2060. });
  2061. }
  2062. $scope.reset = function() {
  2063. if (delegate.isPermission()) {
  2064. if ($scope.historyBackOnSaveOrCancel || policyViewState.state.rootUrl != null) {
  2065. if (policyViewState.state.rootUrl != null) {
  2066. $location.url(policyViewState.state.rootUrl);
  2067. } else {
  2068. $location.url(policyViewState.state.previousUrl);
  2069. }
  2070. } else {
  2071. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/permission/");
  2072. }
  2073. } else {
  2074. if ($scope.historyBackOnSaveOrCancel) {
  2075. $location.url(policyViewState.state.previousUrl);
  2076. } else {
  2077. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/");
  2078. }
  2079. }
  2080. }
  2081. } else {
  2082. service.get({
  2083. realm: realm.realm,
  2084. client : client.id,
  2085. type: delegate.getPolicyType(),
  2086. id: $route.current.params.id
  2087. }, function(data) {
  2088. $scope.originalPolicy = data;
  2089. var policy = angular.copy(data);
  2090. $scope.changed = $scope.historyBackOnSaveOrCancel || PolicyController.isBackNewAssociatedPolicy();
  2091. $scope.policy = angular.copy(policy);
  2092. if (PolicyController.isBackNewAssociatedPolicy()) {
  2093. if (delegate.onRestoreState) {
  2094. delegate.onRestoreState($scope.policy);
  2095. }
  2096. $instance.restoreState($scope);
  2097. } else if (delegate.onInitUpdate) {
  2098. delegate.onInitUpdate($scope.policy);
  2099. }
  2100. $scope.$watch('policy', function() {
  2101. if (!angular.equals($scope.policy, policy)) {
  2102. $scope.changed = true;
  2103. }
  2104. }, true);
  2105. $scope.save = function() {
  2106. $instance.checkNameAvailability(function () {
  2107. if (delegate.onUpdate) {
  2108. delegate.onUpdate();
  2109. }
  2110. service.update({realm : realm.realm, client : client.id, type: $scope.policy.type, id : $scope.policy.id}, $scope.policy, function() {
  2111. if (delegate.isPermission()) {
  2112. if ($scope.historyBackOnSaveOrCancel) {
  2113. $location.url(policyViewState.state.previousUrl);
  2114. } else {
  2115. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/permission/" + $scope.policy.type + "/" + $scope.policy.id);
  2116. }
  2117. $route.reload();
  2118. Notifications.success("The permission has been updated.");
  2119. } else {
  2120. if ($scope.historyBackOnSaveOrCancel) {
  2121. $location.url(policyViewState.state.previousUrl);
  2122. } else {
  2123. $location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/" + $scope.policy.type + "/" + $scope.policy.id);
  2124. }
  2125. $route.reload();
  2126. Notifications.success("The policy has been updated.");
  2127. }
  2128. });
  2129. });
  2130. }
  2131. $scope.reset = function() {
  2132. if ($scope.historyBackOnSaveOrCancel) {
  2133. $location.url(policyViewState.state.previousUrl);
  2134. } else {
  2135. var freshPolicy = angular.copy(data);
  2136. if (delegate.onInitUpdate) {
  2137. delegate.onInitUpdate(freshPolicy);
  2138. }
  2139. $scope.policy = angular.copy(freshPolicy);
  2140. $scope.changed = false;
  2141. }
  2142. }
  2143. });
  2144. $scope.remove = function() {
  2145. Policies.delete(ResourceServerPolicy, $route.current.params.realm, client, $scope, AuthzDialog, $location, Notifications, $route, delegate.isPermission());
  2146. }
  2147. }
  2148. });
  2149. $scope.checkNewNameAvailability = function () {
  2150. $instance.checkNameAvailability(function () {});
  2151. }
  2152. this.checkNameAvailability = function (onSuccess) {
  2153. if (!$scope.policy.name || $scope.policy.name.trim().length == 0) {
  2154. return;
  2155. }
  2156. ResourceServerPolicy.search({
  2157. realm: $route.current.params.realm,
  2158. client: client.id,
  2159. name: $scope.policy.name
  2160. }, function(data) {
  2161. if (data && data.id && data.id != $scope.policy.id) {
  2162. Notifications.error("Name already in use by another policy or permission, please choose another one.");
  2163. } else {
  2164. onSuccess();
  2165. }
  2166. });
  2167. }
  2168. this.restoreState = function($scope) {
  2169. $scope.policy.name = policyViewState.state.policy.name;
  2170. $scope.policy.description = policyViewState.state.policy.description;
  2171. $scope.policy.decisionStrategy = policyViewState.state.policy.decisionStrategy;
  2172. $scope.policy.logic = policyViewState.state.policy.logic;
  2173. $scope.selectedPolicies = policyViewState.state.selectedPolicies;
  2174. if (!$scope.selectedPolicies) {
  2175. $scope.selectedPolicies = [];
  2176. }
  2177. $scope.changed = true;
  2178. var previousPage = policyViewState.state.previousPage;
  2179. if (policyViewState.state.newPolicyName) {
  2180. ResourceServerPolicy.query({
  2181. realm: realm.realm,
  2182. client : client.id,
  2183. permission: false,
  2184. name: policyViewState.state.newPolicyName,
  2185. max : 20,
  2186. first : 0
  2187. }, function(response) {
  2188. for (i = 0; i < response.length; i++) {
  2189. if (response[i].name == policyViewState.state.newPolicyName) {
  2190. response[i].text = response[i].name;
  2191. $scope.selectedPolicies.push(response[i]);
  2192. }
  2193. }
  2194. var rootUrl = policyViewState.state.rootUrl;
  2195. policyViewState.state = {};
  2196. policyViewState.state.previousPage = previousPage;
  2197. policyViewState.state.rootUrl = rootUrl;
  2198. });
  2199. } else {
  2200. var rootUrl = policyViewState.state.rootUrl;
  2201. policyViewState.state = {};
  2202. policyViewState.state.previousPage = previousPage;
  2203. policyViewState.state.rootUrl = rootUrl;
  2204. }
  2205. }
  2206. }
  2207. return PolicyController;
  2208. });
  2209. module.controller('PolicyEvaluateCtrl', function($scope, $http, $route, $location, realm, clients, roles, ResourceServer, client, ResourceServerResource, ResourceServerScope, User, Notifications) {
  2210. $scope.realm = realm;
  2211. $scope.client = client;
  2212. $scope.clients = clients;
  2213. $scope.roles = roles;
  2214. $scope.authzRequest = {};
  2215. $scope.authzRequest.resources = [];
  2216. $scope.authzRequest.context = {};
  2217. $scope.authzRequest.context.attributes = {};
  2218. $scope.authzRequest.roleIds = [];
  2219. $scope.resultUrl = resourceUrl + '/partials/authz/policy/resource-server-policy-evaluate-result.html';
  2220. $scope.addContextAttribute = function() {
  2221. if (!$scope.newContextAttribute.value || $scope.newContextAttribute.value == '') {
  2222. Notifications.error("You must provide a value to a context attribute.");
  2223. return;
  2224. }
  2225. $scope.authzRequest.context.attributes[$scope.newContextAttribute.key] = $scope.newContextAttribute.value;
  2226. delete $scope.newContextAttribute;
  2227. }
  2228. $scope.removeContextAttribute = function(key) {
  2229. delete $scope.authzRequest.context.attributes[key];
  2230. }
  2231. $scope.getContextAttribute = function(key) {
  2232. for (i = 0; i < $scope.defaultContextAttributes.length; i++) {
  2233. if ($scope.defaultContextAttributes[i].key == key) {
  2234. return $scope.defaultContextAttributes[i];
  2235. }
  2236. }
  2237. return $scope.authzRequest.context.attributes[key];
  2238. }
  2239. $scope.getContextAttributeName = function(key) {
  2240. var attribute = $scope.getContextAttribute(key);
  2241. if (!attribute.name) {
  2242. return key;
  2243. }
  2244. return attribute.name;
  2245. }
  2246. $scope.defaultContextAttributes = [
  2247. {
  2248. key : "custom",
  2249. name : "Custom Attribute...",
  2250. custom: true
  2251. },
  2252. {
  2253. key : "kc.identity.authc.method",
  2254. name : "Authentication Method",
  2255. values: [
  2256. {
  2257. key : "pwd",
  2258. name : "Password"
  2259. },
  2260. {
  2261. key : "otp",
  2262. name : "One-Time Password"
  2263. },
  2264. {
  2265. key : "kbr",
  2266. name : "Kerberos"
  2267. }
  2268. ]
  2269. },
  2270. {
  2271. key : "kc.realm.name",
  2272. name : "Realm"
  2273. },
  2274. {
  2275. key : "kc.time.date_time",
  2276. name : "Date/Time (MM/dd/yyyy hh:mm:ss)"
  2277. },
  2278. {
  2279. key : "kc.client.network.ip_address",
  2280. name : "Client IPv4 Address"
  2281. },
  2282. {
  2283. key : "kc.client.network.host",
  2284. name : "Client Host"
  2285. },
  2286. {
  2287. key : "kc.client.user_agent",
  2288. name : "Client/User Agent"
  2289. }
  2290. ];
  2291. $scope.isDefaultContextAttribute = function() {
  2292. if (!$scope.newContextAttribute) {
  2293. return true;
  2294. }
  2295. if ($scope.newContextAttribute.custom) {
  2296. return false;
  2297. }
  2298. if (!$scope.getContextAttribute($scope.newContextAttribute.key).custom) {
  2299. return true;
  2300. }
  2301. return false;
  2302. }
  2303. $scope.selectDefaultContextAttribute = function() {
  2304. $scope.newContextAttribute = angular.copy($scope.newContextAttribute);
  2305. }
  2306. $scope.setApplyToResourceType = function() {
  2307. delete $scope.newResource;
  2308. $scope.authzRequest.resources = [];
  2309. }
  2310. $scope.addResource = function() {
  2311. var resource = angular.copy($scope.newResource);
  2312. if (!resource) {
  2313. resource = {};
  2314. }
  2315. delete resource.text;
  2316. if (!$scope.newScopes || (resource._id != null && $scope.newScopes.length > 0 && $scope.newScopes[0].id)) {
  2317. $scope.newScopes = [];
  2318. }
  2319. var scopes = [];
  2320. for (i = 0; i < $scope.newScopes.length; i++) {
  2321. if ($scope.newScopes[i].name) {
  2322. scopes.push($scope.newScopes[i].name);
  2323. } else {
  2324. scopes.push($scope.newScopes[i]);
  2325. }
  2326. }
  2327. resource.scopes = scopes;
  2328. $scope.authzRequest.resources.push(resource);
  2329. delete $scope.newResource;
  2330. delete $scope.newScopes;
  2331. }
  2332. $scope.removeResource = function(index) {
  2333. $scope.authzRequest.resources.splice(index, 1);
  2334. }
  2335. $scope.resolveScopes = function() {
  2336. if ($scope.newResource._id) {
  2337. $scope.newResource.scopes = [];
  2338. $scope.scopes = [];
  2339. ResourceServerResource.scopes({
  2340. realm: $route.current.params.realm,
  2341. client: client.id,
  2342. rsrid: $scope.newResource._id
  2343. }, function (data) {
  2344. $scope.scopes = data;
  2345. });
  2346. }
  2347. }
  2348. $scope.reevaluate = function() {
  2349. if ($scope.authzRequest.entitlements) {
  2350. $scope.entitlements();
  2351. } else {
  2352. $scope.save();
  2353. }
  2354. }
  2355. $scope.showAuthzData = function() {
  2356. $scope.showRpt = true;
  2357. }
  2358. $scope.save = function() {
  2359. $scope.authzRequest.entitlements = false;
  2360. if ($scope.applyResourceType) {
  2361. if (!$scope.newResource) {
  2362. $scope.newResource = {};
  2363. }
  2364. if (!$scope.newScopes || ($scope.newResource._id != null && $scope.newScopes.length > 0 && $scope.newScopes[0].id)) {
  2365. $scope.newScopes = [];
  2366. }
  2367. var scopes = angular.copy($scope.newScopes);
  2368. for (i = 0; i < scopes.length; i++) {
  2369. delete scopes[i].text;
  2370. }
  2371. $scope.authzRequest.resources[0].scopes = scopes;
  2372. }
  2373. $http.post(authUrl + '/admin/realms/'+ $route.current.params.realm + '/clients/' + client.id + '/authz/resource-server/policy/evaluate'
  2374. , $scope.authzRequest).then(function(response) {
  2375. $scope.evaluationResult = response.data;
  2376. $scope.showResultTab();
  2377. });
  2378. }
  2379. $scope.entitlements = function() {
  2380. $scope.authzRequest.entitlements = true;
  2381. $http.post(authUrl + '/admin/realms/'+ $route.current.params.realm + '/clients/' + client.id + '/authz/resource-server/policy/evaluate'
  2382. , $scope.authzRequest).then(function(response) {
  2383. $scope.evaluationResult = response.data;
  2384. $scope.showResultTab();
  2385. });
  2386. }
  2387. $scope.showResultTab = function() {
  2388. $scope.showResult = true;
  2389. $scope.showRpt = false;
  2390. }
  2391. $scope.showRequestTab = function() {
  2392. $scope.showResult = false;
  2393. $scope.showRpt = false;
  2394. }
  2395. $scope.usersUiSelect = {
  2396. minimumInputLength: 1,
  2397. delay: 500,
  2398. allowClear: true,
  2399. query: function (query) {
  2400. var data = {results: []};
  2401. if ('' == query.term.trim()) {
  2402. query.callback(data);
  2403. return;
  2404. }
  2405. User.query({realm: $route.current.params.realm, search: query.term.trim(), max: 20}, function(response) {
  2406. data.results = response;
  2407. query.callback(data);
  2408. });
  2409. },
  2410. formatResult: function(object, container, query) {
  2411. object.text = object.username;
  2412. return object.username;
  2413. }
  2414. };
  2415. $scope.resourcesUiSelect = {
  2416. minimumInputLength: 1,
  2417. delay: 500,
  2418. allowClear: true,
  2419. id: function(resource){ return resource._id; },
  2420. query: function (query) {
  2421. var data = {results: []};
  2422. if ('' == query.term.trim()) {
  2423. query.callback(data);
  2424. return;
  2425. }
  2426. $scope.query = {
  2427. realm: realm.realm,
  2428. client : client.id,
  2429. name: query.term.trim(),
  2430. deep: false,
  2431. max : 20,
  2432. first : 0
  2433. };
  2434. ResourceServerResource.query($scope.query, function(response) {
  2435. data.results = response;
  2436. query.callback(data);
  2437. });
  2438. },
  2439. formatResult: function(object, container, query) {
  2440. object.text = object.name;
  2441. return object.name;
  2442. }
  2443. };
  2444. $scope.scopesUiSelect = {
  2445. minimumInputLength: 1,
  2446. delay: 500,
  2447. allowClear: true,
  2448. query: function (query) {
  2449. var data = {results: []};
  2450. if ('' == query.term.trim()) {
  2451. query.callback(data);
  2452. return;
  2453. }
  2454. $scope.query = {
  2455. realm: realm.realm,
  2456. client : client.id,
  2457. name: query.term.trim(),
  2458. deep: false,
  2459. max : 20,
  2460. first : 0
  2461. };
  2462. ResourceServerScope.query($scope.query, function(response) {
  2463. data.results = response;
  2464. query.callback(data);
  2465. });
  2466. },
  2467. formatResult: function(object, container, query) {
  2468. object.text = object.name;
  2469. return object.name;
  2470. }
  2471. };
  2472. ResourceServer.get({
  2473. realm : $route.current.params.realm,
  2474. client : client.id
  2475. }, function(data) {
  2476. $scope.server = data;
  2477. });
  2478. $scope.selectUser = function(user) {
  2479. if (!user || !user.id) {
  2480. $scope.selectedUser = null;
  2481. $scope.authzRequest.userId = '';
  2482. return;
  2483. }
  2484. $scope.authzRequest.userId = user.id;
  2485. }
  2486. $scope.reset = function() {
  2487. $scope.authzRequest = angular.copy(authzRequest);
  2488. $scope.changed = false;
  2489. }
  2490. });
  2491. getManageClientId = function(realm) {
  2492. if (realm.realm == masterRealm) {
  2493. return 'master-realm';
  2494. } else {
  2495. return 'realm-management';
  2496. }
  2497. }
  2498. module.controller('RealmRolePermissionsCtrl', function($scope, $http, $route, $location, realm, role, RoleManagementPermissions, Client, Notifications, Dialog, RealmRoleRemover) {
  2499. console.log('RealmRolePermissionsCtrl');
  2500. $scope.role = role;
  2501. $scope.realm = realm;
  2502. $scope.remove = function() {
  2503. RealmRoleRemover.remove($scope.role, realm, Dialog, $location, Notifications);
  2504. };
  2505. RoleManagementPermissions.get({realm: realm.realm, role: role.id}, function(data) {
  2506. $scope.permissions = data;
  2507. $scope.$watch('permissions.enabled', function(newVal, oldVal) {
  2508. if (newVal != oldVal) {
  2509. var param = {enabled: $scope.permissions.enabled};
  2510. $scope.permissions= RoleManagementPermissions.update({realm: realm.realm, role:role.id}, param);
  2511. }
  2512. }, true);
  2513. });
  2514. Client.query({realm: realm.realm, clientId: getManageClientId(realm)}, function(data) {
  2515. $scope.realmManagementClientId = data[0].id;
  2516. });
  2517. });
  2518. module.controller('ClientRolePermissionsCtrl', function($scope, $http, $route, $location, realm, client, role, Client, RoleManagementPermissions, Client, Notifications) {
  2519. console.log('RealmRolePermissionsCtrl');
  2520. $scope.client = client;
  2521. $scope.role = role;
  2522. $scope.realm = realm;
  2523. RoleManagementPermissions.get({realm: realm.realm, role: role.id}, function(data) {
  2524. $scope.permissions = data;
  2525. $scope.$watch('permissions.enabled', function(newVal, oldVal) {
  2526. if (newVal != oldVal) {
  2527. var param = {enabled: $scope.permissions.enabled};
  2528. $scope.permissions = RoleManagementPermissions.update({realm: realm.realm, role:role.id}, param);
  2529. }
  2530. }, true);
  2531. });
  2532. Client.query({realm: realm.realm, clientId: getManageClientId(realm)}, function(data) {
  2533. $scope.realmManagementClientId = data[0].id;
  2534. });
  2535. });
  2536. module.controller('UsersPermissionsCtrl', function($scope, $http, $route, $location, realm, UsersManagementPermissions, Client, Notifications) {
  2537. console.log('UsersPermissionsCtrl');
  2538. $scope.realm = realm;
  2539. var first = true;
  2540. UsersManagementPermissions.get({realm: realm.realm}, function(data) {
  2541. $scope.permissions = data;
  2542. $scope.$watch('permissions.enabled', function(newVal, oldVal) {
  2543. if (newVal != oldVal) {
  2544. var param = {enabled: $scope.permissions.enabled};
  2545. $scope.permissions = UsersManagementPermissions.update({realm: realm.realm}, param);
  2546. }
  2547. }, true);
  2548. });
  2549. Client.query({realm: realm.realm, clientId: getManageClientId(realm)}, function(data) {
  2550. $scope.realmManagementClientId = data[0].id;
  2551. });
  2552. });
  2553. module.controller('ClientPermissionsCtrl', function($scope, $http, $route, $location, realm, client, Client, ClientManagementPermissions, Notifications) {
  2554. $scope.client = client;
  2555. $scope.realm = realm;
  2556. ClientManagementPermissions.get({realm: realm.realm, client: client.id}, function(data) {
  2557. $scope.permissions = data;
  2558. $scope.$watch('permissions.enabled', function(newVal, oldVal) {
  2559. if (newVal != oldVal) {
  2560. var param = {enabled: $scope.permissions.enabled};
  2561. $scope.permissions = ClientManagementPermissions.update({realm: realm.realm, client: client.id}, param);
  2562. }
  2563. }, true);
  2564. });
  2565. Client.query({realm: realm.realm, clientId: getManageClientId(realm)}, function(data) {
  2566. $scope.realmManagementClientId = data[0].id;
  2567. });
  2568. });
  2569. module.controller('IdentityProviderPermissionCtrl', function($scope, $http, $route, $location, realm, identityProvider, Client, IdentityProviderManagementPermissions, Notifications) {
  2570. $scope.identityProvider = identityProvider;
  2571. $scope.realm = realm;
  2572. IdentityProviderManagementPermissions.get({realm: realm.realm, alias: identityProvider.alias}, function(data) {
  2573. $scope.permissions = data;
  2574. $scope.$watch('permissions.enabled', function(newVal, oldVal) {
  2575. if (newVal != oldVal) {
  2576. var param = {enabled: $scope.permissions.enabled};
  2577. $scope.permissions = IdentityProviderManagementPermissions.update({realm: realm.realm, alias: identityProvider.alias}, param);
  2578. }
  2579. }, true);
  2580. });
  2581. Client.query({realm: realm.realm, clientId: getManageClientId(realm)}, function(data) {
  2582. $scope.realmManagementClientId = data[0].id;
  2583. });
  2584. });
  2585. module.controller('GroupPermissionsCtrl', function($scope, $http, $route, $location, realm, group, GroupManagementPermissions, Client, Notifications) {
  2586. $scope.group = group;
  2587. $scope.realm = realm;
  2588. Client.query({realm: realm.realm, clientId: getManageClientId(realm)}, function(data) {
  2589. $scope.realmManagementClientId = data[0].id;
  2590. });
  2591. GroupManagementPermissions.get({realm: realm.realm, group: group.id}, function(data) {
  2592. $scope.permissions = data;
  2593. $scope.$watch('permissions.enabled', function(newVal, oldVal) {
  2594. if (newVal != oldVal) {
  2595. var param = {enabled: $scope.permissions.enabled};
  2596. $scope.permissions = GroupManagementPermissions.update({realm: realm.realm, group: group.id}, param);
  2597. }
  2598. }, true);
  2599. });
  2600. });