CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
2.3 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import json
  17. api_token = cs_form.get("api_token", None)
  18. path = cs_form.get("path", None)
  19. name1 = cs_form.get("username1", None)
  20. name2 = cs_form.get("username2", None)
  21. error = None
  22. if api_token is None:
  23. error = "api_token is required"
  24. elif name1 is None:
  25. error = "username1 is required"
  26. elif name2 is None:
  27. error = "username2 is required"
  28. try:
  29. path = opath = json.loads(path)
  30. except:
  31. error = "invalid path: %s" % path
  32. if error is None:
  33. output = csm_api.get_user_information(
  34. globals(), api_token=api_token, course=path[0]
  35. )
  36. if output["ok"]:
  37. uinfo = output["user_info"]
  38. if "groups" not in uinfo["permissions"] and "admin" not in uinfo["permissions"]:
  39. error = "Permission Denied"
  40. if error is None:
  41. ctx = csm_loader.generate_context(opath)
  42. gnames = ctx.get("cs_group_names", list(map(str, range(100))))
  43. groups = csm_groups.list_groups(ctx, path)
  44. secnum = csm_groups.get_section(ctx, path[0], name1)
  45. sec2 = csm_groups.get_section(ctx, path[0], name2)
  46. if secnum != sec2:
  47. error = "Users are in different sections! (%s and %s)" % (secnum, sec2)
  48. else:
  49. taken = groups.get(secnum, {})
  50. while len(gnames) > 0 and gnames[0] in taken:
  51. gnames.pop(0)
  52. error = csm_groups.add_to_group(ctx, path, name1, gnames[0])
  53. if error is None:
  54. error = csm_groups.add_to_group(ctx, path, name2, gnames[0])
  55. if error is not None:
  56. output = {"ok": False, "error": error}
  57. else:
  58. output = {"ok": True}
  59. cs_handler = "raw_response"
  60. content_type = "application/json"
  61. response = json.dumps(output)